Skip to content

Instantly share code, notes, and snippets.

@theShadow89
Last active December 17, 2015 00:58
Show Gist options
  • Save theShadow89/5524526 to your computer and use it in GitHub Desktop.
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.
<?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