search
Homephp教程php手册拼音码表的生成_php基础

/**
  拼音码表的生成:
      启动输入法生成器在逆转换选项卡中装入window的拼音输入法并将其保存为文
  本文件(winpy.txt)备用。

  下面的代码将拼音码表文件(文本)装入到数组$pymb中,结构为:(拼音,(汉字...))
  码表转换时间较长,应转换后另行保存
*/
$filename = "pymb.txt";
if(file_exists($filename)) {
  $fp = fopen($filename,"r");
  $pymb = unserialize(fread($fp,filesize($filename)));
  fclose($fp);
}else {
  $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)       $pymb[$ch] = $py;
    }
  }
  fclose($fp);
  $fp = fopen("pymb.txt","w");
  fwrite($fp,serialize($pymb)."\n");
  fclose($fp);
}
?>
  应用例,给文字加上拼音


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

function get_py($text) {
  global $pymb;
  $i = 0;
  $n = strlen($text);
  $ar = array();
  while($i    $ch = $text[$i++];
    $py = " ";
    if(ord($ch) > 128) {
      $ch .= $text[$i++];
      $py = $pymb[$ch];
    }
    $ar[] = array($ch,$py);
  }
  return $ar;
}

$text = "  拼音码表的生成:
      启动输入法生成器在逆转换选项卡中装入window的拼音输入法并将其保存为文
  本文件(winpy.txt)备用。

  下面的代码将拼音码表文件(文本)装入到数组$pymb中,结构为:(拼音,(汉字...))
  码表转换时间较长,应转换后另行保存
";


$ar = get_py($text);
//print_r($ar);

echo "

";
for($i=0;$i  echo "";
}
echo "";
for($i=0;$i  echo "";
}
echo "
".$ar[$i][1]."
".$ar[$i][0]."
";
?>

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment