如何转义html标签?在json中检索PHP在phonegap android


How to escape html tags ? in json retrived from php in phonegap android

我创建了一个应用程序,我在服务器端使用php。当一个Ajax调用是使其调用php检索数据表,但问题是一些表包含html &特殊字符标记,所以我得到一个错误在Ajax:

05-31 00:59:07.670: D/CordovaLog(872): Server is not responding... Please try again: SyntaxError: Unexpected token <
05-31 00:59:07.670: I/chromium(872): [INFO:CONSOLE(342)] "Server is not responding... Please try again: SyntaxError: Unexpected token <", source: file:///android_asset/www/home.html (342)

不知道如何解决这个问题。

您可以使用escapeHtml,它返回给定纯文本的HTML转义表示。更多信息请参见escapeHtml

或者您可以使用TextUtils.htmlEncode("your json tag")来编码html。

编辑
当你使用phonegap所以你可以使用htmlencoding PHP也作为

htmlentities($str, ENT_QUOTES);

更多信息参见PHP中的htmlentities

在php中编码html并在你的应用程序中解码,你可以参考这个链接
使用PHP的JSON编码和解码函数来处理发送到和来自应用程序的数据