尤川豪   ·  5月前
Exp. 1,630  ·  212 貼文  ·  145 留言

PHP 計算中文與英文字數

$encoding = mb_detect_encoding($text);

$result = array(
    'count_cn' => 0,
    'count_en' => 0,
);

$text_cn  = preg_replace("/[^\p{Han}\?\!\;\.\〜\ー\。\,\「\」\《\、\》\【\】\『\』\:\(\)\(\)\/\・]/u","", $text);

$result['count_cn'] =  mb_strlen($text_cn, $encoding);

$text_en  = preg_replace("/[\'\"]/","", $text);

$text_en  = preg_replace("/[^a-zA-Z\s]/"," ", $text_en);

$result['count_en'] = str_word_count($text_en);
  分享   共 206 次點閱
共有 1 則留言
尤川豪   ·  5月前
Exp. 1,630  ·  212 貼文  ·  145 留言

多語的版本:

$encoding = mb_detect_encoding($text);

$result = array(
    'count_cn' => 0,
    'count_en' => 0,
    'count_jp' => 0,
    'count_es' => 0,
    'count_id' => 0,
    'count_ko' => 0,
);

$text_cn  = preg_replace("/[^\p{Han}\?\!\;\.\〜\ー\。\,\「\」\《\、\》\【\】\『\』\:\(\)\(\)\/\・]/u","", $text);

$result['count_cn'] =  mb_strlen($text_cn, $encoding);

$text_en  = preg_replace("/[\'\"]/","", $text);

$text_en  = preg_replace("/[^a-zA-Z\s]/"," ", $text_en);

$result['count_en'] = str_word_count($text_en);

$text_cyrillic  = preg_replace("/[^\p{Cyrillic}\s]/","", $text);
$result['count_ru'] = str_word_count($text_cyrillic);

//$pattern_jp = "[^\p{Hiragana}\p{Katakana}\]";
$text_jp  = preg_replace("/[^\p{Han}\p{Hiragana}\p{Katakana}\.\〜\ー\。\,\「\」\《\、\》\【\】\『\』\:\(\)\(\)\/\・]/u","", $text );
//$this->count_jp = mb_strlen($text_jp, $encoding);
$result['count_jp'] =  mb_strlen($text_jp, $encoding);

$text_ko  = preg_replace("/[^\p{Han}\p{Hangul}\?\!\;\.\〜\ー\。\,\「\」\《\、\》\【\】\『\』\:\(\)\(\)\/\・]/u","", $text);
$result['count_ko'] =  mb_strlen($text_ko, $encoding);

$text_ru  = preg_replace("/[^\x{0430}-\x{044F}\x{0410}-\x{042F}\s]/u"," ", $text);

$result['count_ru'] = count(preg_split('/\s+/', $text_ru));

$result['count_ru_literra'] = $result['count_ru'];

$result['count_es'] = str_word_count($text_en);

$result['count_id'] = str_word_count($text_en);

return $result;
 
您的留言
尤川豪
Exp. 1,630  ·  212 貼文  ·  145 留言

關於作者

Devs.tw 作者,喜歡分享&建造新東西的工程師。

歡迎在 Facebook 追蹤我!不定期分享有趣技術文章!

  查看個人檔案