1

The character is UTF8 encoded, like..

"\u676f\u845b"

How to convert it back to normal UTF8 string in PHP?

Howard
  • 19,215
  • 35
  • 112
  • 184

2 Answers2

2

The simple approach would be to wrap your string into double quotes and let json_decode convert the \u0000 escapes. (Which happen to be Javascript string syntax.)

 $str = json_decode("\"$str\"");

Seems to be asian letters: 杯葛 (It's already UTF-8 when json_decode returns it.)

(Source)

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Yes! This is actually the correct answer. json decoding packages are well-maintained and cared for, some function that was copied/pasted to stackoverflow is not. – HoldOffHunger Nov 09 '17 at 18:49
1

http://webarto.com/83/php-unicode_decode-5.3 demo: http://ideone.com/AtY0v

function decode_encoded_utf8($string){
    return preg_replace_callback('#\\\\u([0-9a-f]{4})#ism', function($matches) { return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE"); }, $string);
}
 
echo unicode_decode('\u676f\u845b'); # 杯葛
T.Todua
  • 53,146
  • 19
  • 236
  • 237
Dejan Marjanović
  • 19,244
  • 7
  • 52
  • 66