php语言 百文网手机站

php实现utf-8转unicode函数分享

时间:2021-03-05 12:53:32 php语言 我要投稿

php实现utf-8转unicode函数分享

  php实现utf-8转unicode函数如何操作呢?需要的朋友可以参考下,代码很简单,功能却很实用,推荐给大家。希望对大家有所帮助!

  奉上代码先:

  复制代码 代码如下:

  public function utf8_unicode($str) {

  $unicode = array();

  $values = array();

  $lookingFor = 1;

  for ($i = 0; $i < strlen( $str ); $i++ ) {

  $thisValue = ord( $str[ $i ] );

  if ( $thisValue < ord('A') ) {

  // exclude 0-9

  if ($thisValue >= ord('0') && $thisValue <= ord('9')) {

  // number

  $unicode[] = chr($thisValue);

  }

  else {

  $unicode[] = '%'.dechex($thisValue);

  }

  } else {

  if ( $thisValue < 128) {

  $unicode[] = $str[ $i ];

  } else {

  if ( count( $values ) == 0 ) {

  $lookingFor = ( $thisValue < 224 ) ? 2 : 3;

  }

  $values[] = $thisValue;

  if ( count( $values ) == $lookingFor ) {

  $number = ( $lookingFor == 3 ) ?

  ( ( $values[0] % 16 ) * 4096 ) + ( ( $values[1] % 64 ) * 64 ) + ( $values[2] % 64 ):

  ( ( $values[0] % 32 ) * 64 ) + ( $values[1] % 64 );

  $number = dechex($number);

  $unicode[] = (strlen($number)==3)?"\u0".$number:"\u".$number;

  $values = array();

  $lookingFor = 1;

  } // if

  } // if

  }

  } // for

  return implode("",$unicode);

  }

【php实现utf-8转unicode函数分享】相关文章:

php字符转URL地址的函数09-06

PHP与UTF-8的最佳实践要点09-05

PHP数组函数知识04-03

PHP函数知识总结03-31

PHP基本函数介绍09-08

PHP常用函数汇总09-05

PHP常用函数总结08-17

PHP网络操作函数讲解04-11

PHP中iconv函数知识09-20

关于PHP数组函数知识09-20