Possible Duplicate:
How to convert text to unicode code point like \u0054\u0068\u0069\u0073 using php?
I’m trying to convert all characters that can’t fit into a 7-bit ANSI character into an escaped form, \uN, where N is its decimal value. Here’s what I’ve come up with:
private static function escape($str) {
return preg_replace_callback('~[\\x{007F}-\\x{FFFF}]~u',function($m){return '\\u'.ord($m[0]);},$str);
}
I’ve tried it with characters like Gamma,
echo self::escape('Γ');
But I get \u206 back out instead of \u915. I can’t figure out where I’m going wrong… ideas?
Actually, it appears that either the ord() function doesn’t give me the value or I want, or maybe the encoding on my .php file is wrong?
I had to refresh my memory on exactly how UTF-8 works, but here is a
utf8_ord()function, and a complementingutf8_chr(). Thechr()is lifted pretty much verbatim from my answer here.