开发者

converting C style unicode escapes to htmlentities

开发者 https://www.devze.com 2023-02-04 00:21 出处:网络
I want to change every C style unicode char into html entity. I\'ve written this function to do that:

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;
öß
0

精彩评论

暂无评论...
验证码 换一张
取 消