Oct
24
2020
php获取字符串宽度
作者:
绝缘体.. 发布:
2020-10-24 14:27 分类:
未分类 阅读:
抢沙发
php获取字符串宽度。
/**
* 获取字符串宽度,半角字符1=1,非半角角字符1=2
* @param string $str
* @return float|int
*/
function strwidth($str) {
$charcount = mb_strlen($str, 'utf-8');
$pattern = '/[[:print:][:space:]]/u';
$halfcount = 0;
if (preg_match_all($pattern, $str, $_match)) {
$halfcount = sizeof($_match[0]);
}
return $halfcount + ($charcount - $halfcount) * 2;
}
微信扫一扫,打赏作者吧~