1

Possible Duplicate:
How to get the character from unicode value in PHP?
PHP: Convert unicode codepoint to UTF-8

How can I convert a unicode character such as %u05E1 to a normal character via PHP?

The chr function not covering it and I am looking for something similar.

Community
  • 1
  • 1
jazz
  • 143
  • 1
  • 3
  • 8

2 Answers2

17

"%uXXXX" is a non-standard scheme for URL-encoding Unicode characters. Apparently it was proposed but never really used. As such, there's hardly any standard function that can decode it into an actual UTF-8 sequence.

It's not too difficult to do it yourself though:

$string = '%u05E1%u05E2';
$string = preg_replace('/%u([0-9A-F]+)/', '&#x$1;', $string);
echo html_entity_decode($string, ENT_COMPAT, 'UTF-8');

This converts the %uXXXX notation to HTML entity notation &#xXXXX;, which can be decoded to actual UTF-8 by html_entity_decode. The above outputs the characters "סע" in UTF-8 encoding.

deceze
  • 510,633
  • 85
  • 743
  • 889
0

Use hexdec to convert it to it's decimal representation first.

echo chr(hexdec("05E1"));
var_dump(hexdec("%u05E1") == hexdec("05E1")); //true
Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
  • 2
    `chr` only handles code points defined in ASCI, it won't handle multi-byte Unicode characters. The above `echo`s "?". – deceze Sep 01 '11 at 23:17