php语言

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

时间:2025-04-09 19:19:14 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如何实现Unicode和Utf-8编码相互转换07-28

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

分享php过滤XSS攻击的函数05-21

php制作unicode解码工具08-02

php实现求相对时间函数11-06

PHP与UTF-8的最佳实践09-15

浅析PHP中的UNICODE编码与解码10-23

php使用ftp函数实现简单上传功能10-31

php自定义函数实现汉字分割替换06-01