(转自村子sunggsun)
/**
* utf-8编码字符串截取函数
* @param string $string 要截取的字符串
* @param int $start 起始位置
* @param int $length 截取长度
* @param author sunggsun
* @date: 2004-12-3
*/
function utf8_substr($string, $start, $length=-1) {
switch ($length) {
case 0:
return ”;
case -1:
return $string;
}
preg_match_all(“/([x80-xff]{0,3}|[^x80-xff]?)/is”, $string, $arr);
$rs = array();
foreach($arr[0] as $key=>$value) {
if (trim($value) != ”) {
$rs[] = $value;
}
}
$end = $start+$length;
$len = 0;
$str = ”;
foreach ($rs as $key=>$value) {
$l = $len+strlen($value);
if ($l >= $start && !isset($s)) {
$s = $len;
}
if ($l >= $end && !isset($e)) {
$e = $l;
break;
}
$len = $l;
}
$l = $e – $s;
return substr(implode(”, $rs), $s, $l);
}
$str = “这a是3一b个UTF-8字符串*&^……%¥”;
echo $str . ”
rn”;
$len = strlen($str);
for ($i=0; $i<$len; $i++) {
for ($n=0; $n<=$len-$i; $n++) {
if ($i < 10) {
$a = " " . $i;
}
if ($n < 10) {
$b = " " . $n;
}
echo $a . "," . $b . ":" . utf8_substr($str, $i, $n) . "
rn";
}
}
?>
No Responses (yet)
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.