不支持mb_substr函数的环境中使用了mb_substr函数会出现“Fatal error: Call to undefined function mb_substr()”的提示,下面的函数是定义的替代mb_substr的函数,作用是一样的,用于不支持mb_substr的地方。
// Patch in multibyte support
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);
}
}
utf-8字符串截取时会出现乱码
$a = ‘上次ab登录时间: 2010-05-28 08:48:31′;
var_dump( mb_substr($a, 2, 3) );
试试( mb_substr($a, 2, 3,’utf-8′)
清明时节雨纷纷…
Pingback: » SEO WordPress-人生若只如初見
Pingback: » Centos下安装php的mbstring扩展-78wd.com