Home > Backend Development > PHP Tutorial > 给文字加上拼音_PHP

给文字加上拼音_PHP

WBOY
Release: 2016-06-01 12:36:17
Original
1345 people have browsed it

/**
  拼音码表的生成:
      启动输入法生成器在逆转换选项卡中装入window的拼音输入法并将其保存为文
  本文件(winpy.txt)备用。
  
  下面的代码将拼音码表文件(文本)装入到数组$pymb中,结构为:(拼音,(汉字...))
  码表转换时间较长,应转换后另行保存
*/
$filename = "winpy.txt";
$fp = fopen($filename,"r");
$old = "";
$ar = array();
$pymb = array();
while(! feof($fp)) {
  $buffer = fgets($fp,128);
  sscanf($buffer,"%2s%s",$ch,$py);
  if($ch >= "啊" && ord($py)     if($py != $old) {
      if($old != "") {
        $pymb[] = array(py => $old,hz => $ar);
      }
      $old = $py;
      $ar = array();
    }
    $ar[] = $ch;
  }
}
fclose($fp);
?>
  应用例,给文字加上拼音


/**
  应用例,给文字加上拼音。为简化起见,假定全为中文
*/

$text = "应用例,给文字加上拼音";
echo "

";
for($i=0;$i   $ch = substr($text,$i,2);
  $k = " ";
  for($p=0;$p     if(in_array($ch,$pymb[$p][hz])) {
      $k = $pymb[$p][py];
      break;
    }
  }
  echo "";
}
echo "";
for($i=0;$i   $ch = substr($text,$i,2);
  echo "";
}
echo "
$k
$ch
";
?>
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template