- PHP Snippet: How to decode unicode in PHP strings (`ሴ`)
- Method 2: Decoding using Unicode Encoding in PHP 7+ #
- # Unicode Support in PHP
- # How to use :
- # Output :
- # Converting Unicode characters to their numeric value and/or HTML entities using PHP
- # How to use :
- # Output :
- # Intl extention for Unicode support
- html_entity_decode
- Parameters
- Return Values
- PHP Unicode Support in PHP
- Converting Unicode characters to “\uxxxx” format using PHP
- How to use :
- Output :
- Converting Unicode characters to their numeric value and/or HTML entities using PHP
- How to use :
- Output :
- Intl extention for Unicode support
PHP Snippet: How to decode unicode in PHP strings (`ሴ`)
The json_decode function can automatically convert Unicode strings in PHP:
$text = 'Elon \u2018Technoking\u2019 Musk';
echo json_decode('"' . $text . '"'); // prints "Elon 'Technoking' Musk"
You can also create a helper function for this:
function unicode_decode(string $text): string
return json_decode('"' . $text . '"');
>
$text = 'Elon \u2018Technoking\u2019 Musk';
echo unicode_decode($text); // prints "Elon 'Technoking' Musk"
For more information, please refer to this Stackoverflow post.
Method 2: Decoding using Unicode Encoding in PHP 7+ #
Since PHP 7.0, you can use a Unicode escape syntax to represent code points:
echo "\u"; // represents a unicode char.
If you have a string that contains \u9999, you can use a regular expression to add the required brackets:
// The string with Unicode
$text = 'Elon \u2018Technoking\u2019 Musk';
// The regular expression to match Unicode code point escapes
$regex = '/\\\\u([0-9a-fA-F])/';
// Replace the Unicode code point escapes with their corresponding UTF-8 characters
$decoded = preg_replace_callback($regex, function ($match)
// Convert the HTML entity (`&#xXXXX;`) to its UTF-8 character equivalent.
return mb_convert_encoding('' . $match[1] . ';', 'UTF-8', 'HTML-ENTITIES');
>, $text);
echo $decoded; // Outputs "Elon ‘Technoking’ Musk"
For more information, please refer to the PHP documentation.
Again, you can create a helper function as shown above. If you are wondering about whether to use a Snippet such as this or not, you might find my considerations on the VanillaPHP vs. using packages of interest.
Since you’ve made it this far, sharing this article on your favorite social media network would be highly appreciated 💖! For feedback, please ping me on Twitter.
# Unicode Support in PHP
You can use the following code for going back and forward.
if (!function_exists('codepoint_encode')) function codepoint_encode($str) return substr(json_encode($str), 1, -1); > > if (!function_exists('codepoint_decode')) function codepoint_decode($str) return json_decode(sprintf('"%s"', $str)); > >
# How to use :
echo "\nUse JSON encoding / decoding\n"; var_dump(codepoint_encode("我好")); var_dump(codepoint_decode('\u6211\u597d'));
# Output :
Use JSON encoding / decoding string(12) "\u6211\u597d" string(6) "我好"
# Converting Unicode characters to their numeric value and/or HTML entities using PHP
You can use the following code for going back and forward.
if (!function_exists('mb_internal_encoding')) function mb_internal_encoding($encoding = NULL) return ($from_encoding === NULL) ? iconv_get_encoding() : iconv_set_encoding($encoding); > > if (!function_exists('mb_convert_encoding')) function mb_convert_encoding($str, $to_encoding, $from_encoding = NULL) return iconv(($from_encoding === NULL) ? mb_internal_encoding() : $from_encoding, $to_encoding, $str); > > if (!function_exists('mb_chr')) function mb_chr($ord, $encoding = 'UTF-8') if ($encoding === 'UCS-4BE') return pack("N", $ord); > else return mb_convert_encoding(mb_chr($ord, 'UCS-4BE'), $encoding, 'UCS-4BE'); > > > if (!function_exists('mb_ord')) function mb_ord($char, $encoding = 'UTF-8') if ($encoding === 'UCS-4BE') list(, $ord) = (strlen($char) === 4) ? @unpack('N', $char) : @unpack('n', $char); return $ord; > else return mb_ord(mb_convert_encoding($char, 'UCS-4BE', $encoding), 'UCS-4BE'); > > > if (!function_exists('mb_htmlentities')) function mb_htmlentities($string, $hex = true, $encoding = 'UTF-8') return preg_replace_callback('/[\x-\x]/u', function ($match) use ($hex) return sprintf($hex ? '%X;' : '%d;', mb_ord($match[0])); >, $string); > > if (!function_exists('mb_html_entity_decode')) function mb_html_entity_decode($string, $flags = null, $encoding = 'UTF-8') return html_entity_decode($string, ($flags === NULL) ? ENT_COMPAT | ENT_HTML401 : $flags, $encoding); > >
# How to use :
echo "Get string from numeric DEC value\n"; var_dump(mb_chr(50319, 'UCS-4BE')); var_dump(mb_chr(271)); echo "\nGet string from numeric HEX value\n"; var_dump(mb_chr(0xC48F, 'UCS-4BE')); var_dump(mb_chr(0x010F)); echo "\nGet numeric value of character as DEC string\n"; var_dump(mb_ord('ď', 'UCS-4BE')); var_dump(mb_ord('ď')); echo "\nGet numeric value of character as HEX string\n"; var_dump(dechex(mb_ord('ď', 'UCS-4BE'))); var_dump(dechex(mb_ord('ď'))); echo "\nEncode / decode to DEC based HTML entities\n"; var_dump(mb_htmlentities('tchüß', false)); var_dump(mb_html_entity_decode('tchüß')); echo "\nEncode / decode to HEX based HTML entities\n"; var_dump(mb_htmlentities('tchüß')); var_dump(mb_html_entity_decode('tchüß'));
# Output :
Get string from numeric DEC value string(4) "ď" string(2) "ď" Get string from numeric HEX value string(4) "ď" string(2) "ď" Get numeric value of character as DEC int int(50319) int(271) Get numeric value of character as HEX string string(4) "c48f" string(3) "10f" Encode / decode to DEC based HTML entities string(15) "tchüß" string(7) "tchüß" Encode / decode to HEX based HTML entities string(15) "tchüß" string(7) "tchüß"
# Intl extention for Unicode support
Native string functions are mapped to single byte functions, they do not work well with Unicode. The extentions iconv and mbstring offer some support for Unicode, while the Intl-extention offers full support. Intl is a wrapper for the facto de standard ICU library, see http://site.icu-project.org
ICU offers full Internationalization of which Unicode is only a smaller part. You can do transcoding easily:
\UConverter::transcode($sString, 'UTF-8', 'UTF-8'); // strip bad bytes against attacks
But, do not dismiss iconv just yet, consider:
\iconv('UTF-8', 'ASCII//TRANSLIT', "Cliënt"); // output: "Client"
html_entity_decode
html_entity_decode() is the opposite of htmlentities() in that it converts HTML entities in the string to their corresponding characters.
More precisely, this function decodes all the entities (including all numeric entities) that a) are necessarily valid for the chosen document type — i.e., for XML, this function does not decode named entities that might be defined in some DTD — and b) whose character or characters are in the coded character set associated with the chosen encoding and are permitted in the chosen document type. All other entities are left as is.
Parameters
A bitmask of one or more of the following flags, which specify how to handle quotes and which document type to use. The default is ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 .
Constant Name | Description |
---|---|
ENT_COMPAT | Will convert double-quotes and leave single-quotes alone. |
ENT_QUOTES | Will convert both double and single quotes. |
ENT_NOQUOTES | Will leave both double and single quotes unconverted. |
ENT_SUBSTITUTE | Replace invalid code unit sequences with a Unicode Replacement Character U+FFFD (UTF-8) or � (otherwise) instead of returning an empty string. |
ENT_HTML401 | Handle code as HTML 4.01. |
ENT_XML1 | Handle code as XML 1. |
ENT_XHTML | Handle code as XHTML. |
ENT_HTML5 | Handle code as HTML 5. |
An optional argument defining the encoding used when converting characters.
If omitted, encoding defaults to the value of the default_charset configuration option.
Although this argument is technically optional, you are highly encouraged to specify the correct value for your code if the default_charset configuration option may be set incorrectly for the given input.
The following character sets are supported:
Charset | Aliases | Description |
---|---|---|
ISO-8859-1 | ISO8859-1 | Western European, Latin-1. |
ISO-8859-5 | ISO8859-5 | Little used cyrillic charset (Latin/Cyrillic). |
ISO-8859-15 | ISO8859-15 | Western European, Latin-9. Adds the Euro sign, French and Finnish letters missing in Latin-1 (ISO-8859-1). |
UTF-8 | ASCII compatible multi-byte 8-bit Unicode. | |
cp866 | ibm866, 866 | DOS-specific Cyrillic charset. |
cp1251 | Windows-1251, win-1251, 1251 | Windows-specific Cyrillic charset. |
cp1252 | Windows-1252, 1252 | Windows specific charset for Western European. |
KOI8-R | koi8-ru, koi8r | Russian. |
BIG5 | 950 | Traditional Chinese, mainly used in Taiwan. |
GB2312 | 936 | Simplified Chinese, national standard character set. |
BIG5-HKSCS | Big5 with Hong Kong extensions, Traditional Chinese. | |
Shift_JIS | SJIS, SJIS-win, cp932, 932 | Japanese |
EUC-JP | EUCJP, eucJP-win | Japanese |
MacRoman | Charset that was used by Mac OS. | |
» | An empty string activates detection from script encoding (Zend multibyte), default_charset and current locale (see nl_langinfo() and setlocale() ), in this order. Not recommended. |
Note: Any other character sets are not recognized. The default encoding will be used instead and a warning will be emitted.
Return Values
Returns the decoded string.
PHP Unicode Support in PHP
Converting Unicode characters to “\uxxxx” format using PHP
You can use the following code for going back and forward.
if (!function_exists('codepoint_encode')) < function codepoint_encode($str) < return substr(json_encode($str), 1, -1); >> if (!function_exists('codepoint_decode')) < function codepoint_decode($str) < return json_decode(sprintf('"%s"', $str)); >>
How to use :
echo "\nUse JSON encoding / decoding\n"; var_dump(codepoint_encode("我好")); var_dump(codepoint_decode('\u6211\u597d'));
Output :
Use JSON encoding / decoding string(12) "\u6211\u597d" string(6) "我好"
Converting Unicode characters to their numeric value and/or HTML entities using PHP
You can use the following code for going back and forward.
if (!function_exists('mb_internal_encoding')) < function mb_internal_encoding($encoding = NULL) < return ($from_encoding === NULL) ? iconv_get_encoding() : iconv_set_encoding($encoding); >> if (!function_exists('mb_convert_encoding')) < function mb_convert_encoding($str, $to_encoding, $from_encoding = NULL) < return iconv(($from_encoding === NULL) ? mb_internal_encoding() : $from_encoding, $to_encoding, $str); >> if (!function_exists('mb_chr')) < function mb_chr($ord, $encoding = 'UTF-8') < if ($encoding === 'UCS-4BE') < return pack("N", $ord); >else < return mb_convert_encoding(mb_chr($ord, 'UCS-4BE'), $encoding, 'UCS-4BE'); >> > if (!function_exists('mb_ord')) < function mb_ord($char, $encoding = 'UTF-8') < if ($encoding === 'UCS-4BE') < list(, $ord) = (strlen($char) === 4) ? @unpack('N', $char) : @unpack('n', $char); return $ord; >else < return mb_ord(mb_convert_encoding($char, 'UCS-4BE', $encoding), 'UCS-4BE'); >> > if (!function_exists('mb_htmlentities')) < function mb_htmlentities($string, $hex = true, $encoding = 'UTF-8') < return preg_replace_callback('/[\x-\x]/u', function ($match) use ($hex) < return sprintf($hex ? '&#x%X;' : '&#%d;', mb_ord($match[0])); >, $string); > > if (!function_exists('mb_html_entity_decode')) < function mb_html_entity_decode($string, $flags = null, $encoding = 'UTF-8') < return html_entity_decode($string, ($flags === NULL) ? ENT_COMPAT | ENT_HTML401 : $flags, $encoding); >>
How to use :
echo "Get string from numeric DEC value\n"; var_dump(mb_chr(50319, 'UCS-4BE')); var_dump(mb_chr(271)); echo "\nGet string from numeric HEX value\n"; var_dump(mb_chr(0xC48F, 'UCS-4BE')); var_dump(mb_chr(0x010F)); echo "\nGet numeric value of character as DEC string\n"; var_dump(mb_ord('ď', 'UCS-4BE')); var_dump(mb_ord('ď')); echo "\nGet numeric value of character as HEX string\n"; var_dump(dechex(mb_ord('ď', 'UCS-4BE'))); var_dump(dechex(mb_ord('ď'))); echo "\nEncode / decode to DEC based HTML entities\n"; var_dump(mb_htmlentities('tchüß', false)); var_dump(mb_html_entity_decode('tchüß')); echo "\nEncode / decode to HEX based HTML entities\n"; var_dump(mb_htmlentities('tchüß')); var_dump(mb_html_entity_decode('tchüß'));
Output :
Get string from numeric DEC value string(4) "ď" string(2) "ď" Get string from numeric HEX value string(4) "ď" string(2) "ď" Get numeric value of character as DEC int int(50319) int(271) Get numeric value of character as HEX string string(4) "c48f" string(3) "10f" Encode / decode to DEC based HTML entities string(15) "tchüß" string(7) "tchüß" Encode / decode to HEX based HTML entities string(15) "tchüß" string(7) "tchüß"
Intl extention for Unicode support
Native string functions are mapped to single byte functions, they do not work well with Unicode. The extentions iconv and mbstring offer some support for Unicode, while the Intl-extention offers full support. Intl is a wrapper for the facto de standard ICU library, see http://site.icu-project.org for detailed information that is not available on http://php.net/manual/en/book.intl.php . If you can not install the extention, have a look at an alternative implemention of Intl from the Symfony framework.
ICU offers full Internationalization of which Unicode is only a smaller part. You can do transcoding easily:
\UConverter::transcode($sString, 'UTF-8', 'UTF-8'); // strip bad bytes against attacks
But, do not dismiss iconv just yet, consider:
\iconv('UTF-8', 'ASCII//TRANSLIT', "Cliënt"); // output: "Client"