php-汉字拼音首字母工具类,php-汉字工具类_PHP教程
程序员文章站
2022-04-09 19:35:19
...
php-汉字拼音首字母工具类,php-汉字工具类
php /** * 汉字拼音首字母工具类 * 注: 英文的字串:不变返回(包括数字) eg .abc123 => abc123 * 中文字符串:返回拼音首字符 eg. 测试字符串 => CSZFC * 中英混合串: 返回拼音首字符和英文 eg. 我i我j => WIWJ * eg. * $py = new str2PY(); * * $result = $py->getInitials('周杰伦'); * * //获取首字母 * $result = $py->getFirstString('abc'); //A * $resutl = $py->getFirstString("周杰伦"); //Z * */ class str2py { private $_pinyins = array( 176161 => 'A', 176197 => 'B', 178193 => 'C', 180238 => 'D', 182234 => 'E', 183162 => 'F', 184193 => 'G', 185254 => 'H', 187247 => 'J', 191166 => 'K', 192172 => 'L', 194232 => 'M', 196195 => 'N', 197182 => 'O', 197190 => 'P', 198218 => 'Q', 200187 => 'R', 200246 => 'S', 203250 => 'T', 205218 => 'W', 206244 => 'X', 209185 => 'Y', 212209 => 'Z', ); private $_charset = null; /** * 构造函数, 指定需要的编码 default: utf-8 * 支持utf-8, gb2312 * * @param unknown_type $charset */ public function __construct($charset = 'utf-8') { $this->_charset = $charset; } /** * 中文字符串 substr * * @param string $str * @param int $start * @param int $len * @return string */ private function _msubstr($str, $start, $len) { $start = $start * 2; $len = $len * 2; $strlen = strlen($str); $result = ''; for ($i = 0; $i $strlen