views:

150

answers:

3
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!

+2  A: 

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.

Bombe
+1 I would only add this is part of the spec http://json.org/
Peter Bailey
A: 

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

easement
+2  A: 

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.

Gumbo