php的substr有unicode,PHP: UTF-8兼容的substr()函数

把Nucleus改成了PHP编码后,发现一些功能,如RSS,LastComments插件等等由于要截取前…个字符做预览,结果造成了从UTF-8字符的中间截断,出现错误,这个函数可以很好的改善这个功能。

更好的实现方法当然是使用官方的mb_substr,但是需要在编译的时候指定参数,我等使用虚拟主机的只好用这个方法解决了。

function utf8_substr($str,$start) {

/*

UTF-8 version of substr(), for people who can't use mb_substr() like me.

Length is not the count of Bytes, but the count of UTF-8 Characters

Author: Windix Feng

Bug report to: windix(AT)gmail.com, http://www.douzi.org

- History -

1.0 2004-02-01 Initial Version

2.0 2004-02-01 Use PREG instead of STRCMP and cycles, SPEED UP!

*/

preg_match_all("/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/", $str, $ar);

if(func_num_args() >= 3) {

$end = func_get_arg(2);

return join("",array_slice($ar[0],$start,$end));

} else {

return join("",array_slice($ar[0],$start));

}

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值