自己写的php中文截取函数mb_strlen和mb_substr

2025-05-29 0 102

众所周知,php 自带的 strlen 与 substr 函数没法处理中文字符,于是,我们会用 mb_ 系列函数替代。但是,没有 mbstring 库怎么办?这就需要我们自己写一个来替代了,废话不多说,先上代码:

复制代码 代码如下:


if ( !function_exists('mb_strlen') ) {
function mb_strlen ($text, $encode) {
if ($encode=='UTF-8') {
return preg_match_all('%(?:
[\\x09\\x0A\\x0D\\x20-\\x7E] # ASCII
| [\\xC2-\\xDF][\\x80-\\xBF] # non-overlong 2-byte
| \\xE0[\\xA0-\\xBF][\\x80-\\xBF] # excluding overlongs
| [\\xE1-\\xEC\\xEE\\xEF][\\x80-\\xBF]{2} # straight 3-byte
| \\xED[\\x80-\\x9F][\\x80-\\xBF] # excluding surrogates
| \\xF0[\\x90-\\xBF][\\x80-\\xBF]{2} # planes 1-3
| [\\xF1-\\xF3][\\x80-\\xBF]{3} # planes 4-15
| \\xF4[\\x80-\\x8F][\\x80-\\xBF]{2} # plane 16
)%xs',$text,$out);
}else{
return strlen($text);
}
}
}

/* from Internet, author unknown */
if (!function_exists('mb_substr')) {
function mb_substr($str, $start, $len = '', $encoding="UTF-8"){
$limit = strlen($str);

for ($s = 0; $start > 0;–$start) {// found the real start
if ($s >= $limit)
break;

if ($str[$s] <= "\\x7F")
++$s;
else {
++$s; // skip length

while ($str[$s] >= "\\x80" && $str[$s] <= "\\xBF")
++$s;
}
}

if ($len == '')
return substr($str, $s);
else
for ($e = $s; $len > 0; –$len) {//found the real end
if ($e >= $limit)
break;

if ($str[$e] <= "\\x7F")
++$e;
else {
++$e;//skip length

while ($str[$e] >= "\\x80" && $str[$e] <= "\\xBF" && $e < $limit)
++$e;
}
}

return substr($str, $s, $e – $s);
}
}

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 自己写的php中文截取函数mb_strlen和mb_substr https://www.kuaiidc.com/102396.html

相关文章

发表评论
暂无评论