How to make jscon_encode work with multibyte characters?

拈花ヽ惹草 提交于 2019-12-13 10:28:43

问题


echo '<a title=' .json_encode("按时间先后进行排序") . '>test</a>';

The above will generate something like "\u6309\u65f6\u95f4\u5148\u540e\u8fdb\u884c\u6392\u5e8f" and it's a mess!


回答1:


No, that’s JSON. JSON encoders are free to copy characters as-is (except for doublequote, backslash, or control characters) or to encode them using the \uxxxx notation. So even while the above is not beautiful, it’s valid JSON and will ensure that the string will be decoded correctly.




回答2:


The title attribute value is not treated as JavaScript. Use json_encode only for converting a PHP type into a JavaScript/JSON expression.

Try this instead:

echo '<a title="按时间先后进行排序">test</a>';

But you would need to send your document with the the same encoding as your title text.




回答3:


have you tried this: http://us.php.net/manual/en/function.json-encode.php#74878



来源:https://stackoverflow.com/questions/1531881/how-to-make-jscon-encode-work-with-multibyte-characters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!