search
Homephp教程php手册从dedecms上拿下来的汉字转拼音函数

能实现常用汉字的转换,生僻汉字会被替换成下划线_
需要一个拼音库,跟脚本放在同一目录下,在附件里。
<?php <br /> //示例<br> echo GetPinyin("魍魉");<br> <br> /**<br>  * 汉字转拼音<br>  * @param string $str 待转换的字符串<br>  * @param string $charset 字符串编码<br>  * @param bool $ishead 是否只提取首字母<br>  * @return string 返回结果<br>  */<br> function GetPinyin($str,$charset="utf-8",$ishead = 0) {<br>     $restr = '';<br>     $str = trim($str);<br>     if($charset=="utf-8"){<br>         $str=iconv("utf-8","gb2312",$str);<br>     }<br>     $slen = strlen($str);<br>     $pinyins=array();<br>     if ($slen          return $str;<br>     }<br>     $fp = fopen('pinyin.dat', 'r');<br>     while (!feof($fp)) {<br>         $line = trim(fgets($fp));<br>         $pinyins[$line[0] . $line[1]] = substr($line, 3, strlen($line) - 3);<br>     }<br>     fclose($fp);<br>     <br>     for ($i = 0; $i          if (ord($str[$i]) > 0x80) {<br>             $c = $str[$i] . $str[$i + 1];<br>             $i++;<br>             if (isset($pinyins[$c])) {<br>                 if ($ishead == 0) {<br>                     $restr .= $pinyins[$c];<br>                 } else {<br>                     $restr .= $pinyins[$c][0];<br>                 }<br>             } else {<br>                 $restr .= "_";<br>             }<br>         } else if (preg_match("/[a-z0-9]/i", $str[$i])) {<br>             $restr .= $str[$i];<br>         } else {<br>             $restr .= "_";<br>         }<br>     }<br>     return $restr;<br> }

附件 pinyin.zip ( 19.67 KB 下载:520 次 )

AD:真正免费,域名+虚机+企业邮箱=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 Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Atom editor mac version download

Atom editor mac version download

The most popular open source editor