php 中文字符串截取函数
2014-01-08 15:39
429 查看
下面这二款函数是二款双字节字符串截取函数,那就是针对中文字符串截取了,好了第一款汉字中文截取函数是越级简洁了,后一款复杂但考虑更多一些。
<?
//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] <= "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);
}
}
?>
转载请注明:爱分享 » 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] <= "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);
}
}
?>
转载请注明:爱分享 » php
中文字符串截取函数
相关文章推荐
- php截取中文字符串函数实例
- php字符串截取函数,支持中文截取
- php中计算中文字符串长度、截取中文字符串的函数代码
- php截取中文字符串长度的mb_substr()、mb_strcut()函数 附strlen/mb_strlen
- php自定义中文字符串截取函数substr_for_gb2312及substr_for_utf8示例
- php用正则表达式截取中文字符串的函数
- php中的一个中文字符串截取函数
- php自动截取中文字符串 和 取出html、css、js格式函数
- php截取中文字符串无乱码的函数(主函数:ord()、substr())
- php字符串截取函数(支持中文 utf-8截取)
- PHP中一个中文字符串截取函数
- php中的一个中文字符串截取函数
- php 字符串操作函数substr()截取中文子串乱码问题
- PHP中文字符串截取无乱码方法,php汉字截取函数_preg_split()
- php中支持多种编码的中文字符串截取函数!
- php 中文字符串截取函数-->比较好的
- PHP字符串截取函数(解决中文乱码问题!支持UTF-8和GB2312)
- PHP基础12-自定义函数截取中文字符串
- PHP中文字符串截取函数
- 支持中文的PHP字符串截取函数 (附各语言的字符集编码范围)