php获取字符串宽度

 
更多

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;
}
打赏

本文固定链接: https://www.cxy163.net/archives/3221 | 绝缘体

该日志由 绝缘体.. 于 2020年10月24日 发表在 未分类 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: php获取字符串宽度 | 绝缘体
关键字: , , , ,
【上一篇】
【下一篇】

php获取字符串宽度:等您坐沙发呢!

发表评论


快捷键:Ctrl+Enter