Home  >  Article  >  php教程  >  php把汉字转化成拼音

php把汉字转化成拼音

PHP中文网
PHP中文网Original
2016-05-25 17:08:221172browse


=’5.0′) ? array_combine($_TDataKey, $_TDataValue) : array_combine($_TDataKey, $_TDataValue);
  //arsort:对数组进行逆向排序并保持索引关系
  //reset:将数组的内部指针指向第一个单元
  arsort($_Data);
  reset($_Data);
 
  /**
   *    假如编码不是gb2312,则启用utf-8
   */
  if($_Code != ‘gb2312′) $_String = _U2_Utf8_Gb($_String);
  $_Res = ”;
  for($i=0; $i160) {
    $_Q = ord(substr($_String, ++$i, 1));
    $_P = $_P*256 + $_Q – 65536;
   }
   $_Res .= _Pinyin($_P, $_Data);
  }
  return preg_replace(“/[^a-z0-9]*/”, ”, $_Res);
 }
 
 function _Pinyin($_Num, $_Data) {
  if($_Num>0 && $_Num<160 )
   return chr($_Num);
  elseif($_Num-10247)
   return ”;
  else {
   foreach($_Data as $k=>$v) {
    if($v<=$_Num)
     break;
   }
   return $k;
  }
 }
 
 function _U2_Utf8_Gb($_C) {
  $_String = ”;
  if($_C < 0×80) {
   $_String .= $_C;
  }
  elseif($_C < 0×800) {
   $_String .= chr(0xC0 | $_C>>6);
   $_String .= chr(0×80 | $_C & 0x3F);
  }
  elseif($_C < 0×10000) {
   $_String .= chr(0xE0 | $_C>>12);
   $_String .= chr(0×80 | $_C>>6 & 0x3F);
   $_String .= chr(0×80 | $_C & 0x3F);
  }
  elseif($_C < 0×200000) {
   $_String .= chr(0xF0 | $_C>>18);
   $_String .= chr(0×80 | $_C>>12 & 0x3F);
   $_String .= chr(0×80 | $_C>>6 & 0x3F);
   $_String .= chr(0×80 | $_C & 0x3F);
  }
  return @iconv(‘UTF-8′, ‘GB2312//IGNORE’, $_String);
 }
 
 
 echo Pinyin(“我的博客”);
?>

                       

           

2. 

=’5.0′) ? array_combine($_TDataKey, $_TDataValue) : array_combine($_TDataKey, $_TDataValue);
  //arsort:对数组进行逆向排序并保持索引关系
  //reset:将数组的内部指针指向第一个单元
  arsort($_Data);
  reset($_Data);
 
  /**
   *    假如编码不是gb2312,则启用utf-8
   */
  if($_Code != ‘gb2312′) $_String = _U2_Utf8_Gb($_String);
  $_Res = ”;
  for($i=0; $i160) {
    $_Q = ord(substr($_String, ++$i, 1));
    $_P = $_P*256 + $_Q – 65536;
   }
   $_Res .= _Pinyin($_P, $_Data);
  }
  return preg_replace(“/[^a-z0-9]*/”, ”, $_Res);
 }
 
 function _Pinyin($_Num, $_Data) {
  if($_Num>0 && $_Num<160 )
   return chr($_Num);
  elseif($_Num-10247)
   return ”;
  else {
   foreach($_Data as $k=>$v) {
    if($v<=$_Num)
     break;
   }
   return $k;
  }
 }
 
 function _U2_Utf8_Gb($_C) {
  $_String = ”;
  if($_C < 0×80) {
   $_String .= $_C;
  }
  elseif($_C < 0×800) {
   $_String .= chr(0xC0 | $_C>>6);
   $_String .= chr(0×80 | $_C & 0x3F);
  }
  elseif($_C < 0×10000) {
   $_String .= chr(0xE0 | $_C>>12);
   $_String .= chr(0×80 | $_C>>6 & 0x3F);
   $_String .= chr(0×80 | $_C & 0x3F);
  }
  elseif($_C < 0×200000) {
   $_String .= chr(0xF0 | $_C>>18);
   $_String .= chr(0×80 | $_C>>12 & 0x3F);
   $_String .= chr(0×80 | $_C>>6 & 0x3F);
   $_String .= chr(0×80 | $_C & 0x3F);
  }
  return @iconv(‘UTF-8′, ‘GB2312//IGNORE’, $_String);
 }
 
 
 echo Pinyin(“我的博客”);
?>

           

       

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn