php 中文字符串截取函数
2013-09-05 19:25
756 查看
下面这二款函数是二款双字节字符串截取函数,那就是针对中文字符串截取了,好了第一款汉字中文截取函数是越级简洁了,后一款复杂但考虑更多一些。<?
//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教程 中文字符串截取函数
/*
下面这二款函数是二款双字节字符串截取函数,那就是针对中文字符串截取了,好了第一款汉字中文截取函数是越级简洁了,后一款复杂但考虑更多一些。
*/
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汉字截取函数_preg_split()
- 几个支持中文的PHP字符串截取函数
- 收藏几个支持中文的PHP字符串截取函数
- php字符串截取函数(支持中文 utf-8截取)
- PHP中一个中文字符串截取函数
- php 字符串操作函数substr()截取中文子串乱码问题
- 支持中文的PHP字符串截取函数 (附各语言的字符集编码范围)
- PHP中的字符串截取函数,取子符串,以保证中文不出现乱码
- PHP 中文字符串截取无乱码的方法以及php汉字截取函数_preg_split()
- php截取中文字符串函数实例
- php自定义中文字符串截取函数substr_for_gb2312及substr_for_utf8示例
- PHP基础12-自定义函数截取中文字符串
- PHP截取含中文的混合字符串长度的函数
- php中计算中文字符串长度、截取中文字符串的函数代码
- PHP 中文字符串截取函数(UTF-8)有效
- php中支持多种编码的中文字符串截取函数!
- php中高性能中文字符串截取函数分享
- php中计算中文字符串长度、截取中文字符串的函数代码
- PHP的两个常用函数截取中文字符串无乱码与获取客户端IP地址
- 多个PHP中文字符串截取函数