Last active
December 17, 2015 00:58
-
-
Save theShadow89/5524526 to your computer and use it in GitHub Desktop.
This Function convert special character to Unicode. I used it for match the words on tweet.
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 | |
function toUnicode($str) { | |
if (!mb_check_encoding($str, 'UTF-8')) { | |
trigger_error('String is not encoded in UTF-8'); | |
return false; | |
} | |
return preg_replace_callback('/./u', function ($m) { | |
//convert the char on UTF-16 because the ord function not work very well on UTF-8 | |
$s = mb_convert_encoding($m[0], 'UCS-2LE', 'UTF-8'); | |
$ord = ord($s); | |
if ($ord <= 127) { | |
return $m[0]; | |
} else { | |
//coding on Unicode all char greather than 127 on ASCII table | |
return sprintf('\\\u%04x', $ord); | |
} | |
}, $str); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment