简体   繁体   中英

How do I convert Unicode escape sequences to text in PHP?

I have this Unicode sequence: \お\は\よ\う\ご\ざ\い\ま\す . How do I convert it into text?

$unicode = '\u304a\u306f\u3088\u3046\u3054\u3056\u3044\u307e\u3059';

I tried:

echo $utf8-decode(unicode);

and I tried:

echo mb_convert_encoding($unicode , 'US-ASCII', 'UTF-8');

and I tried:

echo htmlentities($unicode , ENT_COMPAT, "UTF-8");

but none of these functions convert the sequence into the corresponding Japanese text.

The issue here is that the string is not unicode. It is an escape sequence used to note down unicode by means of ASCII characters (so 7bit save).

There is a simply trick to use the phps json decoder for this:

<?php
$sequence = '\u304a\u306f\u3088\u3046\u3054\u3056\u3044\u307e\u3059';
print_r(json_decode('["'.$sequence.'"]'));

The output is:

Array
(
    [0] => おはようございます
)

This means you can define a simple convenience function:

<?php
$sequence = '\u304a\u306f\u3088\u3046\u3054\u3056\u3044\u307e\u3059';

function decode($payload) {
  return array_pop(json_decode('["'.$payload.'"]'));
}

echo decode($sequence);

You want to add error handling and escaping of json specific control characters inside the payload. This simply example is just meant to point you into the right direction...

Have fun!

PHP 7+

As of PHP 7, you can use the Unicode codepoint escape syntax to do this.

echo "\\u{304a}\\u{306f}\\u{3088}\\u{3046}\\u{3054}\\u{3056}\\u{3044}\\u{307e}\\u{3059}"; outputs おはようございます .

$unicode = '\u304a\u306f\u3088\u3046\u3054\u3056\u3044\u307e\u3059';
$json = sprintf('"%s"',$unicode); # build json string

$utf8_str = json_decode ( $json, true ); # json decode
echo $utf8_str; # おはようございます

See Json string

在此输入图像描述

Transliterator class from intl extension can handle the convertion with its predefined Hex-Any identifier :

$in = '\u304a\u306f\u3088\u3046\u3054\u3056\u3044\u307e\u3059';
$out = transliterator_create('Hex-Any')->transliterate($in);
var_dump($out); # string(27) "おはようございます"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM