public function getAllPY($chinese, $delimiter = '', $length = 0) {
$py = $this->zh_to_pys($chinese, $delimiter); if($length) { $py = substr($py, 0, $length); } return $py; } //拼音首个字母 public function getFirstPY($chinese){ $result = '' ; for ($i=0; $i<strlen($chinese); $i++) { $p = ord(substr($chinese,$i,1)); if ($p>160) { $q = ord(substr($chinese,++$i,1)); $p = $p*256 + $q - 65536; } $result .= substr($this->zh_to_py($p),0,1); } return $result ; } //-------------------中文转拼音--------------------------------// private function zh_to_py($num, $blank = '') { if($num>0 && $num<160 ) { return chr($num); } elseif ($num<-20319||$num>-10247) { return $blank; } else { foreach ($this->pylist as $py => $code) { if($code > $num) break; $result = $py; } return $result; } } private function zh_to_pys($chinese, $delimiter = ' ', $first=0){ $result = array(); for($i=0; $i<strlen($chinese); $i++) { $p = ord(substr($chinese,$i,1)); if($p>160) { $q = ord(substr($chinese,++$i,1)); $p = $p*256 + $q - 65536; } $result[] = $this->zh_to_py($p); if ($first) { return $result[0]; } } return implode($delimiter, $result); } } ?>
//测试 $PingYing = new GetPingYing(); echo '<br>',$PingYing->getFirstPY('免费模版网'),'<br>';//mfmbw echo $PingYing->getAllPY('羽免费模版网'),'<br>'; //mianfeimubanwang