GBK:
function mysubstr($str, $start, $len) {
$tmpstr = "";
$strlen = $start + $len;
for($i = 0; $i < $strlen; $i++) {
if(ord(substr($str, $i, 1)) > 0xa0) {
$tmpstr .= substr($str, $i, 2);
$i++;
} else
$tmpstr .= substr($str, $i, 1);
}
return$tmpstr;
}
?>
今天发现(2014-02-11)上面方法不靠谱,还是出现了乱码,用下面这个就可以了:
function sysSubStr($String,$Length,$Append = false)
{
if (strlen($String) <= $Length )
{
return $String;
}
else
{
$I = 0;
while ($I < $Length)
{
$StringTMP = substr($String,$I,1);
if ( ord($StringTMP) >=224 )
{
$StringTMP = substr($String,$I,3);
$I = $I + 3;
}
elseif( ord($StringTMP) >=192 )
{
$StringTMP = substr($String,$I,2);
$I = $I + 2;
}
else
{
$I = $I + 1;
}
$StringLast[] = $StringTMP;
}
$StringLast = implode("",$StringLast);
if($Append)
{
$StringLast .= "...";
}
return $StringLast;
}
}
UTF-8:
function utfSubstr($str, $position, $length,$type=1){
$startPos = strlen($str);
$startByte = 0;
$endPos = strlen($str);
$count = 0;
for($i=0; $i
if($count>=$position && $startPos>$i){
$startPos = $i;
$startByte = $count;
}
if(($count-$startByte) >= $length) {
$endPos = $i;
break;
}
$value = ord($str[$i]);
if($value > 127){
$count++;
if($value>=192 && $value<=223) $i++;
elseif($value>=224 && $value<=239) $i = $i + 2;
elseif($value>=240 && $value<=247) $i = $i + 3;
elsereturn self::raiseError("\"$str\" Not a UTF-8 compatible string", 0, __CLASS__, __METHOD__, __FILE__, __LINE__);
}
$count++;
}
if($type==1 && ($endPos-6)>$length){
returnsubstr($str, $startPos, $endPos-$startPos);
}
else{
returnsubstr($str, $startPos, $endPos-$startPos);
}
}
?>