note 39535 added to function.substr
| From: | windix at 263 dot net | Date: | Sun, 01 Feb 2004 15:30:09 +0000 |
| Subject: | note 39535 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-64505@lists.php.net to get a copy of this message | ||
Here is a function to handle UTF-8 strings, for someone who can't use mb_substr like me.
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)263.net, http://www.douzi.org/blog
- 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));
}
}
----
Manual Page -- http://www.php.net/manual/en/function.substr.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39535
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39535&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39535&report=yes
Search -- http://master.php.net/manage/user-notes.php