阮一峰的IT笔记:首页 -> 分类 -> PHP 查看所有文章:按分类 | 按月份

PHP: UTF-8兼容的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));
}
}
?>

« 关于PHP中Object对象的笔记 | 首页 | 符合标准的固定在网页中固定位置的广告(转贴) »

About

This page contains a single entry from the blog posted on 2007年12月09日 17:55.

The previous post in this blog was 关于PHP中Object对象的笔记.

The next post in this blog is 符合标准的固定在网页中固定位置的广告(转贴).

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33

Post a comment