php 统计中英文字数/单词数

注意是字数/单词,不是字节/字符数,支持纯中文、纯英文、中英文混输的各种字符数统计。

hello world 你好世界

字数是 6 = 2英文 + 4中文

function count_words($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;
}

你可能感兴趣的:(php字符中英文匹配)