时间:2021-07-01 10:21:17 帮助过:5人阅读
/**
* 得到utf8 的长度
* @author mu_rain
* @param String $str
* @return Int
*/
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;
}
------解决方案--------------------
function myLen($str,$startTag='[',$endTag=']',$encoding='utf-8')
{
$st = preg_quote($startTag);
$et = preg_quote($endTag);
return mb_strlen(preg_replace("#{$st}[^{$et}]*{$et}#","~",$str),$encoding);
}
echo myLen("123asd你好[哈哈]");//9
------解决方案--------------------
附赠字符串函数一堆 ,慢用。