I want to change every C
style unicode
char into html
entity. I've written this function to do that:
function ununic开发者_开发技巧ode($text) {
$text = preg_replace('/\\\\u([0-9a-f]{4})/i', '&#x$1;', $text);
return $text;
}
it works good, but ignores second character in sth like that \u00f6\u00df
. ie it will produce: ö\u00df
whats wrong with my regex?
Try adding the g
flag (which allows more than one replacement per line), so that it's this:
$text = preg_replace('/\\\\u([0-9a-f]{4})/ig', '&#x$1;', $text);
Edit:
Your code as written seems to work for me:
php > $text = "\u00f6\u00df";
php > print $text;
\u00f6\u00df
php > $text2 = preg_replace('/\\\\u([0-9a-f]{4})/i', '&#x$1;', $text);
php > print $text2;
öß
精彩评论