PHP Decoding Unicode encoding

Source: Internet
Author: User

This article mainly introduces the PHP decoding Unicode encoding, has a certain reference value, now share to everyone, the need for friends can refer to the

function Unicode_decode ($name) {//method one $name = Str_replace ("\\\\u", "\u", $name);    $json = ' {' str ': '. $name. '} ';    $arr = Json_decode ($json, true);     if (empty ($arr)) return ';    return $arr [' str ']; Method two//convert encoding to convert Unicode encoding into a utf-8 encoding that can be browsed $pattern = '/([\w]+) |      (\\\u ([\w]{4}))/I ';      Preg_match_all ($pattern, $name, $matches);          if (!empty ($matches)) {$name = ';              for ($j = 0; $j < count ($matches [0]), $j + +) {$str = $matches [0][$j];                  if (Strpos ($str, ' \\u ') = = = 0) {$code = Base_convert (substr ($STR, 2, 2), 16, 10);                  $code 2 = Base_convert (substr ($STR, 4), 16, 10);                  $c = Chr ($code). Chr ($code 2);                  $c = Iconv (' UCS-2 ', ' UTF-8 ', $c);              $name. = $c;              } else {$name. = $str;  }}} return $name; }


here's how to find it online:
Scenario A (Stable + recommended):

function Replace_unicode_escape_sequence ($match) {  return mb_convert_encoding (Pack (' h* ', $match [1]), ' UTF-8 ', ' Ucs-2be ');} $name = ' \u65b0\u6d6a\u5fae\u535a '; $str = Preg_replace_callback ('/\\\\u ([0-9a-f]{4})/I ', ' replace_unicode_escape_ Sequence ', $name); Echo $str; Output: Sina Weibo
Let's wrap up the above scenario a (Plan a stable + upgrade + recommended) class helper_tool{  static function Unicodedecode ($data)  {      function Replace_unicode_escape_sequence ($match) {      return mb_convert_encoding (Pack (' h* ', $match [1]), ' UTF-8 ', ' ucs-2be ') ;    }      $rs = Preg_replace_callback ('/\\\\u ([0-9a-f]{4})/I ', ' replace_unicode_escape_sequence ', $data);    return $rs;  }  } Call $name = ' \u65b0\u6d6a\u5fae\u535a '; $data = Helper_tool::unicodedecode ($name); Output Sina Weibo

Scenario B (Recommended):

<?phpfunction Unicodedecode ($name) {$json = ' {' str ': ' '. $name. '} ';  $arr = Json_decode ($json, true);   if (empty ($arr)) return '; return $arr [' str '];} $name = ' \u65b0\u6d6a\u5fae\u535a '; Echo unicodedecode ($name); Output: Sina Weibo 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.