theShadow89
5/6/2013 - 11:04 AM

This Function convert special character to Unicode. I used it for match the words on tweet.

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);
}
?>