function comment_count_word($str)
{
$str = preg_replace('/[\x80-\xff]{1,3}/', ' ', $str, -1, $n);//匹配中文或中文下的符号
$str = preg_replace('/[-\_\.!@#\$%\\\^&\*\)\(\+=\{\}\[\]\/",\'<>~\·`\?:;|]/', ' ', $str, -1, $j);//匹配英文下的符号
$n += $j;
$n += str_word_count($str);
if (preg_match('/\d+/', $str)) {
preg_replace('/\d+/', ' ', $str, -1, $i);
$n += $i;
}
return $n;
}
该方法统计的字数大概和 word 一致,因为 word 统计字数,相连的英文和数字(半角)会计算成一个字,如 all1234 就会统计成一个字。由于我没想到这到底是是怎么一个算法,所以我这边就没法搞了。如果有大佬知道这么搞,求讲解!
继续阅读 PHP 仿 Word 统计文章字数