Last active
February 22, 2017 20:16
-
-
Save Kudratullah/831c04cb90d91b9e385f to your computer and use it in GitHub Desktop.
Convert UniCode Escape Character to UTF-8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Works Perfict | |
function uni2html($string){ | |
//preg_replace(): Compilation failed: PCRE does not support \L, \l, \N{name}, \U, or \u at offset 1. | |
//so can't use $string = preg_replace('/\\u([0-9A-Za-z]+)/', '&#x$1;', $string); directly. | |
$string = explode('\\', $string); | |
$string = implode('%', $string); | |
$string = preg_replace('/%u([0-9A-Za-z]+)/', '&#x$1;', $string); | |
return html_entity_decode($string, ENT_COMPAT, 'UTF-8'); | |
} | |
// this also works perfict | |
function unicode2html($string){ | |
$string = explode('\\', $string); | |
$string = implode('%', $string); | |
$string = preg_replace('/%u([0-9A-Za-z]+)/', '&#x$1;', $string); | |
return mb_convert_encoding($string, 'UTF-8', 'HTML-ENTITIES'); | |
} | |
// Works Perfict | |
function unicode_escape_sequences($str){ | |
//$working = json_encode($str); | |
$working = preg_replace('/\\\u([0-9a-z]{4})/', '&#x$1;', $str); | |
return $working;// json_decode($working); | |
} | |
// Works Perfict | |
function replace_unicode_escape_sequence($match) { | |
return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE'); | |
} | |
echo $str = preg_replace_callback('/\\\\u([0-9a-f]{4})/i', 'replace_unicode_escape_sequence', $string); | |
// Works Perfict | |
echo preg_replace_callback('/(?:\\\\u[0-9a-fA-Z]{4})+/', function ($v){ | |
$v = strtr($v[0], array('\\u' => '')); | |
return mb_convert_encoding(pack('H*', $v), 'UTF-8', 'UTF-16BE'); | |
}, $string); | |
// One of the easiest way & works really well. | |
// json_decode function automatically convert unicode character into readable html | |
print_r(json_decode($string)); | |
// This Also Works | |
$unicodeChar = '\u1000'; | |
echo json_decode('"'.$unicodeChar.'"'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment