I am looking to replace
mb_convert_encoding($string, 'utf-8', 'HTML-ENTITIES');
with something else, because I do not have the mb module installed and cannot install it. Is there anything? I tried utf8_decode($string), but that didn't work.
This is different from the question listed, because it tries to do the reverse.
I want to take a string, for example, with ä in UTF8 and convert it into ä or its HTML entity as &#num;
If you don't have mb_convert_encoding
you can try using a preg_replace_callback
to UCS-4
encode the string
and then use bin2hex
, something like:
$string = "★ PHP UTF-8 Sucks! ★";
$entity = preg_replace_callback('/[\x{80}-\x{10FFFF}]/u', function ($m) {
$char = current($m);
$utf = iconv('UTF-8', 'UCS-4', $char);
return sprintf("&#x%s;", ltrim(strtoupper(bin2hex($utf)), "0"));
}, $string);
echo $entity;
★ PHP UTF-8 Sucks! ★
You may consider the use of the MBString Polyfill library. This library adds MBString methods even if you do not have the extension installed on your platform.
I can fix my problem with this:
htmlspecialchars_decode( html_entity_decode( html_entity_decode( $string, ENT_QUOTES | ENT_XML1, 'UTF-8' ) ) );