自动摘要
正在生成中……
php 内置的 str_word_count
函数不是很靠谱,更好的方式是安装 Intl 扩展
。
function utf8_word_count($string, $mode = 0) {
static $it = NULL;
if (is_null($it)) {
$it = IntlBreakIterator::createWordInstance(ini_get('intl.default_locale'));
}
$l = 0;
$it->setText($string);
$ret = $mode == 0 ? 0 : array();
if (IntlBreakIterator::DONE != ($u = $it->first())) {
do {
if (IntlBreakIterator::WORD_NONE != $it->getRuleStatus()) {
$mode == 0 ? ++$ret : $ret[] = substr($string, $l, $u - $l);
}
$l = $u;
} while (IntlBreakIterator::DONE != ($u = $it->next()));
}
return $ret;
}
ref: str_word_count() that works with Eastern languages in PHP?