下面这二款函数是二款双字节字符串截取函数,那就是针对中文字符串截取了,好了第一款汉字中文截取函数是越级简洁了,后一款复杂但考虑更多一些。 ? //php教程 中文字符串截取函数 /* 下面这二款函数是二款双字节字符串截取函数,那就是针对中文字符串截取了
下面这二款函数是二款双字节字符串截取函数,那就是针对中文字符串截取了,好了第一款汉字中文截取函数是越级简洁了,后一款复杂但考虑更多一些。
//php教程 中文字符串截取函数
/*
下面这二款函数是二款双字节字符串截取函数,那就是针对中文字符串截取了,好了第一款汉字中文截取函数是越级简洁了,后一款复杂但考虑更多一些。
*/
function substr($str = ”, $offset = 0, $len = 0){ $len || ($len = strlen($str)); preg_match_all(‘/./us’, $str, $result); return implode(”, array_slice($result[0], $offset, $len)); }
//方法二
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] ++$s;
else {
++$s; // skip lengthwhile ($str[$s] >= "x80" && $str[$s] ++$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] ++$e;
else {
++$e;//skip lengthwhile ($str[$e] >= "x80" && $str[$e] ++$e;
}
}return substr($str, $s, $e – $s);
}
}
?>
爱分享 ? php 中文字符串截取函数
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号