<?php
//******截取字符串GB2312
function Sub_gb($in,$num){
$pos=0;
$out="";
while($pos<strlen(trim($in))){
$c=substr(trim($in),$pos,1);
if($c=="\n") break;
if(ord($c)>128){
$out.=$c;
$pos++;
$c=substr(trim($in),$pos,1);
$out.=$c;
}else{
$out.=$c;
}
$pos++;
if($pos>=$num) {
$out.="...";
break;
}
}
return $out;
}
//utf8
function Sub($str, $length,$position=0,$type=1){
$startPos = strlen($str);
$startByte = 0;
$endPos = strlen($str);
$count = 0;
for($i=0; $i<strlen($str); $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;
else return self::raiseError("\"$str\" Not a UTF-8 compatible string", 0, __CLASS__, __METHOD__, __FILE__, __LINE__);
}
$count++;
}
if($type==1 && ($endPos-6)>$length){
return substr($str, $startPos, $endPos-$startPos)."...";
}else{
return substr($str, $startPos, $endPos-$startPos);
}
}
?>