PHP unicodeと通常文字列の相互変換

7615 ワード

unicode回転文字列
方法1:json
  /**
   * unicode    ,  json  
   * @param $str
   * @return string
   */
  function unicode_decode_by_json($str)
  {
    $json = '{"str":"' . $str . '"}';
    $arr = json_decode($json, true);
    if (empty($arr)) return '';
    return $arr['str'];
  }

 
方法2:
  /**
   * unicode   
   * @param $data
   * @return null|string|string[]
   */
  function unicode_decode($data)
  {
    $rs = preg_replace_callback('/\\\\u([0-9a-f]{4})/i', 'replace_unicode_escape_sequence', $data);
    return $rs;
  }

  function replace_unicode_escape_sequence($match)
  {
    return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
  }

 
 
文字列回転unicode
 
  /**
   * @param  string $str      ,       
   * @return string
   */
  function get_unicode($str)
  {
    $bin_str = '';
    $arr = is_array($str) ? $str : str_split($str);//          ,  $arr   array(228, 189, 160)
    foreach ($arr as $value) $bin_str .= decbin(ord($value));//             ,$bin_str   111001001011110110100000,     " "
    $bin_str = preg_replace('/^.{4}(.{4}).{2}(.{6}).{2}(.{6})$/', '$1$2$3', $bin_str);//    , $bin_str   0100111101100000,     " "

    $unicode = dechex(bindec($bin_str));//  unicode    

    $_sup = '';
    for ($i = 0; $i < 4 - strlen($unicode); $i++) $_sup .= '0';//      0

    return '\\u' . $_sup . $unicode; //   \u    
  }

  /**
   *       unicode
   * @param $str string    /   
   * @return string
   */
  function unicode_encode($str)
  {
    $_arr_str = preg_split('/(?$str);//        (     )

    $_ret_unicode = '';
    foreach ($_arr_str as $_str) $_ret_unicode .= get_unicode($_str);

    return $_ret_unicode;
  }

 
テストの効果:
  $_str_test = 'see,        ';
  $_unicode = unicode_encode($_str_test);


  echo $_str_test . ' => ' . $_unicode, '

'; echo $_unicode . ' => ' . unicode_decode($_unicode), '

'; echo $_unicode . ' => ' . unicode_decode_by_json($_unicode), '

';