php utf字符串处理类
<?class Utf8 {
/**
* This function get utf8 string length
* @param : string $str
* @return :length
*/
static function utf8WordStrlen($str) {
return iconv_strlen($str);
}
/**
* 统计utf8字符,中文按照1个字计算
*/
static function strlen_utf8 ($str)
{
$i = 0;
$count = 0;
$len = strlen ($str);
while ($i < $len)
{
$chr = ord ($str[$i]);
$count++;
$i++;
if ($i >= $len)
break;
if ($chr & 0x80)
{
$chr <<= 1;
while ($chr & 0x80)
{
$i++;
$chr <<= 1;
}
}
}
return $count;
}
/**
* 中文按照1个字计算,进行截取
*/
static function strsub_utf8 ($str,$start_position,$length) {
$i = 0;
$count = 0;
$len = strlen ($str);
$end_position = $len;
while ($i < $len)
{
$chr = ord ($str[$i]);
$count++;
$i++;
if ($i >= $len)
break;
if ($chr & 0x80)
{
$chr <<= 1;
while ($chr & 0x80)
{
$i++;
$chr <<= 1;
}
}
if($count>=$length) {
$end_position = $i;
break;
}
}
return substr($str,$start_position,$end_position-$start_position);
}
/**
* 统计utf8字符,中文按照2个字计算
*/
static function utf8Strlen($str) {
$count = 0;
for($i = 0; $i < strlen($str); $i++)
{
$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
die('Not a UTF-8 compatible string');
}
$count++;
}
return $count;
}
static function utf8StrSub($str,$position,$length) {
$start_position = strlen($str);
$start_byte = 0;
$end_position = strlen($str);
$count = 0;
for($i = 0; $i < strlen($str); $i++) {
if($count >= $position && $start_position > $i) {
$start_position = $i;
$start_byte = $count;
}
if(($count-$start_byte)>=$length && $count >= $position) {
$end_position = $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
die('Not a UTF-8 compatible string');
}
$count++;
}
return(substr($str,$start_position,$end_position-$start_position));
}
}
?>
页:
[1]