蝙蝠岛资源网 Design By www.hbtsch.com
本文实例讲述了PHP切割汉字的常用方法。分享给大家供大家参考,具体如下:
<"";
$strlen = $start + $len;
for ($i = 0; $i < $strlen; $i++) {
if (ord(substr($str, $i, 1)) > 0xa0) //ord()函数返回字符串的第一个字符的ASCII值
{
$tmpstr .= substr($str, $i, 2);
$i++;
} else {
$tmpstr .= substr($str, $i, 1);
}
}
return $tmpstr;
}
/*----------------------------第二种方法-----------------------------------*/
//截取的是UTF-8字符串
function utf_substr($str, $len)
{
$new_str = [];
for ($i = 0; $i < $len; $i++) {
$tem_str = substr($str, 0, 1);
if (ord($tem_str > 127)) {
$i++;
if ($i < $len) {
$new_str[] = substr($str, 0, 3);
$str = substr($str, 3);
}
} else {
$new_str[] = substr($str, 0, 1);
$str = substr($str, 1);
}
}
return join($new_str);//join()函数把数组元素组合为一个字符串
}
/*-------------------------------------第三种方法(UTF-8)--------------------------------*/
function cutstr($string, $length)
{
preg_match_all("/[\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]/", $string, $info);
$wordscut = "";
$j = 0;
for ($i = 0; $i < count($info[0]); $i++) {
$wordscut .= $info[0][$i];
$j = ord($info[0][$i]) > 127 "...";
}
}
return join('', $info[0]);
}
$string = "312哈哈,这个组合很难切割哦";
echo cutstr($string, 10);
/*---------------------------------下面是曾经用过的截取第三个的字符串的------------------------------*/
// $name1 = mysql_result($my_rst,0,"name");
// $name = preg_match("/([1-9][0-9]+)/",$name1,$r);
// $name = $r[0];
// if($name == ""){
// $name=preg_replace('#^("...";//超过长度时在尾处加上省略号
}
return $returnstr;
}
/*--------------------第五种方法(UTF-8)---------------------------------------------*/
function FSubstr($title, $start, $len = "", $magic = true)
{
if ($len == "") $len = strlen($title);
if ($start != 0) {
$startv = ord(substr($title, $start, 1));
if ($startv >= 128) {
if ($startv < 192) {
for ($i = $start - 1; $i > 0; $i--) {
$tempv = ord(substr($title, $i, 1));
if ($tempv >= 192) break;
}
$start = $i;
}
}
}
if (strlen($title) <= $len) return substr($title, $start, $len);
$alen = 0;
$blen = 0;
$realnum = 0;
$length = 0;
for ($i = $start; $i < strlen($title); $i++) {
$ctype = 0;
$cstep = 0;
$cur = substr($title, $i, 1);
if ($cur == "&") {
if (substr($title, $i, 4) == "<") {
$cstep = 4;
$length += 4;
$i += 3;
$realnum++;
if ($magic) {
$alen++;
}
} elseif (substr($title, $i, 4) == ">") {
$cstep = 4;
$length += 4;
$i += 3;
$realnum++;
if ($magic) {
$alen++;
}
} elseif (substr($title, $i, 5) == "&") {
$cstep = 5;
$length += 5;
$i += 4;
$realnum++;
if ($magic) {
$alen++;
}
} elseif (substr($title, $i, 6) == """) {
$cstep = 6;
$length += 6;
$i += 5;
$realnum++;
if ($magic) {
$alen++;
}
} elseif (preg_match("/&#(\d+);", substr($title, $i), $match)) {
$cstep = strlen($match[0]);
$length += strlen($match[0]);
$i += strlen($match[0]) - 1;
$realnum++;
if ($magic) {
$blen++;
$ctype = 1;
}
}
} else {
if (ord($cur) >= 252) {
$cstep = 6;
$length += 6;
$i += 5;
$realnum++;
if ($magic) {
$blen++;
$ctype = 1;
}
} elseif (ord($cur) >= 248) {
$cstep = 5;
$length += 5;
$i += 4;
$realnum++;
if ($magic) {
$ctype = 1;
$blen++;
}
} elseif (ord($cur) >= 240) {
$cstep = 4;
$length += 4;
$i += 3;
$realnum++;
if ($magic) {
$blen++;
$ctype = 1;
}
} elseif (ord($cur) >= 224) {
$cstep = 3;
$length += 3;
$i += 2;
$realnum++;
if ($magic) {
$ctype = 1;
$blen++;
}
} elseif (ord($cur) >= 192) {
$ctype = 2;
$length += 2;
$i += 1;
$realnum++;
if ($magic) {
$blen++;
$ctype = 1;
}
} elseif (ord($cur) >= 128) {
$length += 1;
} else {
$cstep = 1;
$length += 1;
$realnum++;
if ($magic) {
if (ord($cur) >= 65 && ord($cur) <= 90) {
$blen++;
} else {
$alen++;
}
}
}
}
if ($magic) {
if (($blen * 2 + $alen) == ($len * 2)) break;
if (($blen * 2 + $alen) == ($len * 2) + 1) {
if ($ctype == 1) {
$length -= $cstep;
break;
} else {
break;
}
}
} else {
if ($realnum == $len) break;
}
}
unset($cur);
unset($alen);
unset($blen);
unset($realnum);
unset($ctype);
unset($cstep);
return substr($title, $start, $length);
}
function utf8Substr($str, $from, $len)
{
return preg_replace('#^("你哈珀niad1纳斯达wop asdni你爱谁都没阿斯顿撒旦12ccs- sd";
$title = utf8Substr($title, 0, 15);
echo $title;
"_blank" href="//www.jb51.net/Special/459.htm">PHP编码与转码操作技巧汇总》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php常用函数与技巧总结》及《PHP错误与异常处理方法总结》
希望本文所述对大家PHP程序设计有所帮助。
标签:
PHP,切割汉字
蝙蝠岛资源网 Design By www.hbtsch.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
蝙蝠岛资源网 Design By www.hbtsch.com
暂无PHP切割汉字的常用方法实例总结的评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
2025年10月30日
2025年10月30日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]