很多站长朋友们都不太清楚php怎么快速获得汉字,今天小编就来给大家整理php怎么快速获得汉字,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php如何获取随机的中文字??自动生成4个字的中文字? 2、 PHP中怎样只获取汉字 3、 php 怎么截取汉字字符串? 4、 如何在php中进行汉字查找? 5、 php批量获取首字母(汉字、数字、英文) 6、 PHP自定义函数获取汉字首字母的方法 php如何获取随机的中文字??自动生成4个字的中文字?应该有个字库,可以在网上随便复制一篇文章下来,作为字库,最好把里面的标点符号空格什么的都去去掉,就是纯的汉字,然后再随机取
PHP中怎样只获取汉字我这个测试了一下,支持GB2312编码和UTF-8编码,其他编码下思路类似,只是要找下编码的起始值就可以了。
function match_chinese($chars,$encoding='utf8'){
$pattern =($encoding=='utf8')?'/[\x{4e00}-\x{9fa5}]/u':'/[\x80-\xFF]/';
preg_match_all($pattern,$chars,$result);
$temp =join('',$result[0]);
return $temp;
}
调用:
$str ="中文汉字English Chars数字12131321";
//UTF-8编码下
echo match_chinese($str);
//gb2312编码下
echo match_chinese($str,'gb2312');
php 怎么截取汉字字符串??php
$str = '这样一来我的字符串就不会有乱码^_^';
echo "mb_substr:" . mb_substr($str, 0, 7, 'utf-8');
//结果:这样一来我的字
echo "
";
echo "mb_strcut:" . mb_strcut($str, 0, 6, 'utf-8');
//结果:这样
?
首先
1.确保你的Windows/system32下有php_mbstring.dll这个文件,没有就从你Php安装目录extensions里拷入Windows/system32里面。
2.在windows目录下找到php.ini打开编辑,搜索mbstring.dll,找到
;extension=php_mbstring.dll把前面的;号去掉,这样mb_substr函数就可以生效了
mb_strcut函数功能也可以截取字符串长度,下面实例具体看看区别在哪:
mb_substr();
如何在php中进行汉字查找?<?php
$str=array(':','你好:','你好:','hello');
for($i=0;$i<count($str);$i++){
if(ereg('你好:',$str[$i]))echo "$i.匹配<br>";//正则表达式
}
?>
php批量获取首字母(汉字、数字、英文)php批量获取首字母(汉字 数字 英文)
$mysql_server_name= ; //改成自己的mysql数据库服务器
$mysql_username= 用户 ; //改成自己的mysql数据库用户名
$mysql_password= 密码 ; //改成自己的mysql数据库密码
$mysql_database= 数据库 ; //改成自己的mysql数据库名
mysql_connect( $mysql_username $mysql_password) or die( database not access );
mysql_select_db($mysql_database);
mysql_query("SET NAMES utf ");
$equery = " select title from 表 ";
$result =mysql_query($equery );
while ($row = mysql_fetch_array($result MYSQL_BOTH))
{
$title=$row["title"];
if (ord($title)> ) { //汉字开头
echo $letter=getfirstchar($title);
}else if(ord($title)>= and ord($title)<= ){ //数字开头
echo $letter=iconv_substr($title utf );
}else if(ord($title)>= and ord($title)<= ){ //大写英文开头
echo $letter=iconv_substr($title utf );
}else if(ord($title)>= and ord($title)<= ){ //小写英文开头
echo $letter=iconv_substr($title utf );
}
}
function getfirstchar($s ){
$s=iconv("UTF " "gb " $s );
$asc=ord($s{ })* +ord($s{ }) ;
if($asc>= and $asc<= )return "A";
if($asc>= and $asc<= )return "B"; if($asc>= and $asc<= )return "C";
if($asc>= and $asc<= )return "D";
if($asc>= and $asc<= )return "E";
if($asc>= and $asc<= )return "F";
if($asc>= and $asc<= )return "G";
if($asc>= and $asc<= )return "H";
if($asc>= and $asc<= )return "J";
if($asc>= and $asc<= )return "K";
if($asc>= and $asc<= )return "L";
if($asc>= and $asc<= )return "M";
if($asc>= and $asc<= )return "N";
if($asc>= and $asc<= )return "O";
if($asc>= and $asc<= )return "P";
if($asc>= and $asc<= )return "Q";
if($asc>= and $asc<= )return "R";
if($asc>= and $asc<= )return "S";
if($asc>= and $asc<= )return "T";
if($asc>= and $asc<= )return "W";
if($asc>= and $asc<= )return "X";
if($asc>= and $asc<= )return "Y";
if($asc>= and $asc<= )return "Z";
return false;
lishixinzhi/Article/program/PHP/201311/20913
PHP自定义函数获取汉字首字母的方法本文实例讲述了PHP自定义函数获取汉字首字母的方法。分享给大家供大家参考,具体如下:
首字母很重要,可以进行排序使用。
城市列表等等。
<?php
/*
*
Created
on
2016-12-1
*/
function
getFirstCharter($str)
{
if
(empty($str))
{
return
'';
}
$fchar
=
ord($str{0});
if
($fchar
>=
ord('A')
$fchar
<=
ord('z'))
return
strtoupper($str{0});
$s1
=
iconv('UTF-8',
'gb2312',
$str);
$s2
=
iconv('gb2312',
'UTF-8',
$s1);
$s
=
$s2
==
$str
?
$s1
:
$str;
$asc
=
ord($s{0})
*
256
+
ord($s{1})
-
65536;
if
($asc
>=
-20319
$asc
<=
-20284)
return
'A';
if
($asc
>=
-20283
$asc
<=
-19776)
return
'B';
if
($asc
>=
-19775
$asc
<=
-19219)
return
'C';
if
($asc
>=
-19218
$asc
<=
-18711)
return
'D';
if
($asc
>=
-18710
$asc
<=
-18527)
return
'E';
if
($asc
>=
-18526
$asc
<=
-18240)
return
'F';
if
($asc
>=
-18239
$asc
<=
-17923)
return
'G';
if
($asc
>=
-17922
$asc
<=
-17418)
return
'H';
if
($asc
>=
-17417
$asc
<=
-16475)
return
'J';
if
($asc
>=
-16474
$asc
<=
-16213)
return
'K';
if
($asc
>=
-16212
$asc
<=
-15641)
return
'L';
if
($asc
>=
-15640
$asc
<=
-15166)
return
'M';
if
($asc
>=
-15165
$asc
<=
-14923)
return
'N';
if
($asc
>=
-14922
$asc
<=
-14915)
return
'O';
if
($asc
>=
-14914
$asc
<=
-14631)
return
'P';
if
($asc
>=
-14630
$asc
<=
-14150)
return
'Q';
if
($asc
>=
-14149
$asc
<=
-14091)
return
'R';
if
($asc
>=
-14090
$asc
<=
-13319)
return
'S';
if
($asc
>=
-13318
$asc
<=
-12839)
return
'T';
if
($asc
>=
-12838
$asc
<=
-12557)
return
'W';
if
($asc
>=
-12556
$asc
<=
-11848)
return
'X';
if
($asc
>=
-11847
$asc
<=
-11056)
return
'Y';
if
($asc
>=
-11055
$asc
<=
-10247)
return
'Z';
return
null;
}
$firstChar
=
getFirstCharter('脚本之家');
print_r($firstChar);//输出:J
?>
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php常用函数与技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP错误与异常处理方法总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
关于php怎么快速获得汉字的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php怎么快速获得汉字 php获取文本内容的详细内容...