教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 汉字转换成Unicode编码PHP程序

汉字转换成Unicode编码PHP程序

发布时间:2017-10-25   编辑:jiaochengji.com
教程集为您提供汉字转换成Unicode编码PHP程序等资源,欢迎您收藏本站,我们将为您提供最新的汉字转换成Unicode编码PHP程序资源

汉字转换成unicode方法

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1914')"><textarea id="copy1914" rows="10" cols="40" style="display: none;"></textarea></td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1914>

<?php
//将utf8编码的汉字转换为unicode
function htou($c){
 $n = (ord($c[0]) & 0x1f) << 12;
 $n = (ord($c[1]) & 0x3f) << 6;
 $n = ord($c[2]) & 0x3f;
 return $n;
}

//在代码中隐藏utf8格式的字符串
function my_utf8_unicode($str) {
 $encode='';
 for($i=0;$i<strlen($str);$i ){
  if(ord(substr($str,$i,1))> 0xa0){
   $encode.='&#'.htou(substr($str,$i,3)).';';
   $i =2;
  }else{
   $encode.='&#'.ord($str[$i]).';';
  }
 }
 return $encode;
}

echo my_utf8_unicode("哈哈ABC");
?>

汉字转换成unicode方法二

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3281')"><textarea id="copy3281" rows="10" cols="40" style="display: none;"></textarea></td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3281>


function getUnicode($word)
{
 // 转UTF8
 $word0 = iconv('gbk', 'utf-8', $word);
 $word1 = iconv('utf-8', 'gbk', $word0);
 $word =  ($word1 == $word) ? $word0 : $word;
 // 拆分汉字
 preg_match_all('#(?:[x00-x7F]|[xC0-xFF][x80-xBF] )#s', $word, $array, PREG_PATTERN_ORDER);
 $return  = array();
 // 转换
 foreach ($array[0] as $cc)
 {
  $arr = str_split($cc);
  $bin_str = '';
  foreach ($arr as $value)
  {
   $bin_str .= decbin(ord($value));
  }
  $bin_str = preg_replace('/^.{4}(.{4}).{2}(.{6}).{2}(.{6})$/','$1$2$3', $bin_str);
  $return[] = '&#' . bindec($bin_str) . ';';
 }
 
 return implode('', $return);
}

函数用法:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7343')"><textarea id="copy7343" rows="10" cols="40" style="display: none;"></textarea></td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7343>

$word = '一个汉字转换成Unicode四字节编码的PHP函数。';
echo getUnicode($word);

上述将输出如下结果:

&#19968&#20010&#27721&#23383&#36716&#25442&#25104&#65333&#65358
&#65353&#65347&#65359&#65348&#65349&#22235&#23383&#33410&#32534
&#30721&#30340&#80&#72&#80&#20989&#25968&#12290

这一组函数可以将汉字转成unicode编码,也可以将unicode解码成汉字。
将汉字转成Unicode的函数:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6503')"><textarea id="copy6503" rows="10" cols="40" style="display: none;"></textarea></td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6503>

function uni_encode ($word)
{
 $word0 = iconv('gbk', 'utf-8', $word);
 $word1 = iconv('utf-8', 'gbk', $word0);
 $word =  ($word1 == $word) ? $word0 : $word;
    $word = json_encode($word);
    $word = preg_replace_callback('/\\u(w{4})/', create_function('$hex', 'return '&#'.hexdec($hex[1]).';';'), substr($word, 1, strlen($word)-2));
    return $word;
}

对Unicode编码进行解码的函数:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6650')"><textarea id="copy6650" rows="10" cols="40" style="display: none;"></textarea></td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6650>

function uni_decode ($uncode)
{
    $word = json_decode(preg_replace_callback('/&#(d{5});/', create_function('$dec', 'return '\u'.dechex($dec[1]);'), '"'.$uncode.'"'));
    return $word;
}

您可能感兴趣的文章:
php中把unicode编码转化为中文
php汉字unicode编码与解码
php json_encode中文编码的问题
详细阐述PHP环境下如何将GBK编码转成UTF-8格式
汉字转换成Unicode编码PHP程序
php各种编码集 字符集 显示 详解
简单明了!utf8和utf8mb4的区别
PHP把16进制的编码转为中文程序代码
php不使用iconv库进行gb2312与utf-8编码转换的函数
json_encode 中文显示问题解决方法

[关闭]
~ ~