105 行
2.4 KiB
PHP
可执行文件
105 行
2.4 KiB
PHP
可执行文件
<?php
|
|
|
|
namespace Common\Business;
|
|
//[1.5.66]
|
|
/**
|
|
* 汉字转拼音类
|
|
* Class PinYin
|
|
* [
|
|
* 中文字符串转拼音首字母 strChangePin
|
|
* 汉字转拼音的首字母 get_first_letter
|
|
* 汉字转拼音 str2py
|
|
* 获取拼音对应ascll码 pinyin
|
|
* ]
|
|
* @package Common\Business
|
|
*/
|
|
class PinYin
|
|
{
|
|
/**
|
|
* 中文字符串转拼音首字母
|
|
* @param $str 字符串
|
|
* @return string
|
|
*/
|
|
public function strChangePin($str)
|
|
{
|
|
header("Content-type: text/html; charset=utf-8");
|
|
$new_str = '';
|
|
for ($i = 0; $i < mb_strlen($str, 'utf-8'); $i++) {
|
|
$new_str .= $this->get_first_letter(mb_substr($str, $i, 1, 'utf-8'));
|
|
}
|
|
return $new_str;
|
|
}
|
|
|
|
|
|
/**
|
|
* 汉字转拼音的首字母
|
|
* @param
|
|
*[
|
|
* $zh 字符串
|
|
* ]
|
|
* @return string
|
|
*/
|
|
public function get_first_letter($zh)
|
|
{
|
|
$letters = $this->str2py($zh);
|
|
$f_letter = substr($letters, 0, 1);
|
|
return strtoupper($f_letter);
|
|
}
|
|
|
|
|
|
/**
|
|
* 汉字转拼音
|
|
* @param
|
|
*[
|
|
* $s 字符串
|
|
* ]
|
|
* @return string
|
|
*/
|
|
public function str2py($s)
|
|
{
|
|
$s = preg_replace("/\s/is", "_", $s);
|
|
$s = preg_replace("/(|\~|\`|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\-|\+|\=|\{|\}|\[|\]|\||\\|\:|\;|\"|\'|\<|\,|\>|\.|\?|\/)/is", "", $s);
|
|
$py = "";
|
|
$i = 0;
|
|
// 加入这一句,自动识别utf-8
|
|
if (strlen("拼音") > 4) {
|
|
$s = iconv('UTF-8', 'GBK//TRANSLIT//IGNORE', $s);
|
|
}
|
|
for ($i = 0; $i < strlen($s); $i++) {
|
|
if (ord($s[$i]) > 128) {
|
|
$py .= $this->asi2py(ord($s[$i]) + ord($s[$i + 1]) * 256);
|
|
$i++;
|
|
} else {
|
|
$py .= $s[$i];
|
|
}
|
|
/* if (strlen($py) >= 20)
|
|
return $py;*/
|
|
}
|
|
return $py;
|
|
}
|
|
|
|
public function asi2py($a)
|
|
{
|
|
$py = $this->pinyin();
|
|
foreach ($py as $key=>$p) {
|
|
if (array_search($a, $p) === false) {
|
|
|
|
} else {
|
|
return $key;
|
|
}
|
|
// next($py);
|
|
}
|
|
return '';
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取拼音对应ascll码
|
|
* @return array
|
|
*/
|
|
public function pinyin()
|
|
{
|
|
$PinYinClass = new \Common\Lib\PinYin();
|
|
return $PinYinClass->getPinyin();
|
|
}
|
|
} |