zblog
当前位置:首页 > 商学院 > zblog > 正文内容

zblog

php中截取中文字符串

豫唐网络2017-09-20 15:042932

截取GB2312中文字符串

functionmysubstr($str,$start,$len){
$tmpstr="";
$strlen=$start+$len;
for($i=0;$i<$strlen;$i++){
if(ord(substr($str,$i,1))>0xa0){
$tmpstr.=substr($str,$i,2);
$i++;
}else
$tmpstr.=substr($str,$i,1);
}
return$tmpstr;
}

截取utf8编码的多字节字符串

functionutf8Substr($str,$from,$len){
returnpreg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s','$1',$str);
}

UTF-8、GB2312都支持的汉字截取函数

functioncut_str($string,$sublen,$start=0,$code='UTF-8')
{
if($code=='UTF-8'){
$pa="/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
preg_match_all($pa,$string,$t_string);
if(count($t_string[0])-$start>$sublen)returnjoin('',array_slice($t_string[0],$start,$sublen))."...";
returnjoin('',array_slice($t_string[0],$start,$sublen));
}
else{
$start=$start*2;
$sublen=$sublen*2;
$strlen=strlen($string);
$tmpstr='';

for($i=0;$i<$strlen;$i++){
if($i>=$start&&$i<($start+$sublen)){
if(ord(substr($string,$i,1))>129){
$tmpstr.=substr($string,$i,2);
}
else{
$tmpstr.=substr($string,$i,1);
}
}
if(ord(substr($string,$i,1))>129)$i++;
}
if(strlen($tmpstr)<$strlen)$tmpstr.="...";
return$tmpstr;
}
}
$str="abcd需要截取的字符串";
echocut_str($str,8,0,'gb2312');

BugFree 的字符截取函数

functionsysSubStr($String,$Length,$Append=false)
{
if(strlen($String)<=$Length)
{
return$String;
}
else
{
$I=0;
while($I<$Length)
{
$StringTMP=substr($String,$I,1);
if(ord($StringTMP)>=224)
{
$StringTMP=substr($String,$I,3);
$I=$I+3;
}
elseif(ord($StringTMP)>=192)
{
$StringTMP=substr($String,$I,2);
$I=$I+2;
}
else
{
$I=$I+1;
}
$StringLast[]=$StringTMP;
}
$StringLast=implode("",$StringLast);
if($Append)
{
$StringLast.="...";
}
return$StringLast;
}
}
$String="ytecn.com豫唐网络";
$Length="18";
$Append=false;
echosysSubStr($String,$Length,$Append);


扫描二维码推送至手机访问。

版权声明:本文由汤阴县豫唐网络科技有限公司发布,如需转载请注明出处。

本文链接:https://www.ytecn.com/post/71.html

分享给朋友:

相关文章

Call to undefined function mysql_connect()

Call to undefined function mysql_connect()

今天给某个站搬家,zblogphp程序,原站环境是php5.6,这次配的环境是7.1。然后搬家后,提示然后,惯例,先查看数据库账户密码是否正确。经测试,信息无误。然后呢。。发现配置文件好像挺复杂。这个...

zblogphp程序报错后如何获得帮助

zblogphp程序报错后如何获得帮助

zblogphp程序报错后获取帮助分为免费和付费两种。免费帮助方法1开启调试模式(点击打开新链接),将截图发到群内。方法2提交工单或私信把网站信息发群主,等凑够一定数量后,群主开直播查错。付费帮助找群...

zblogphp数据字典zblogphp数据库字典zblogphp数据表字段大全

zblogphp数据字典zblogphp数据库字典zblogphp数据表字段大全

zblogphp系统默认为8张表,分别为category(分类表)、comment(评论表)、config(系统和应用配置表)、member(用户表)、module(模块表)、post(文章表(含...

学习zblog主题模板制作需要提前掌握什么知识

学习zblog主题模板制作需要提前掌握什么知识

学习网站主题模板制作需要提前掌握什么知识?1、学会购买域名2、学会购买服务器(或空间)3、学会安装zblog程序;4、学习并掌握 vscode 代码远程同步功能,可以实现本地操作上面的zblog程序代...

zblogphp插件制作常用功能

zblogphp插件制作常用功能

制作zblogphp插件常用知识点汇总。文档如有描述不清楚、错误或者过时的地方,欢迎留言指出。顾问服务当然,我们也支持付费远程指导。https://www.ytecn.com/post/319.htm...

zblog主题制作教程常用功能

zblog主题制作教程常用功能

制作zblogphp主题常用知识点汇总。文档如有描述不清楚、错误或者过时的地方,欢迎留言指出。本教程内容会不定时更新,转载请标明原帖链接,以免让过时的教程流入网络。温馨提示:请在制作主题的过程中,严格...