str_word_count()函数返回一个数组,该数组包含字符串中的所有单词。除非使用特殊字符,否则它效果很好。在这种情况下,PHP脚本通过查询字符串接收字符串:使用str_word_count()支持特殊字符
当我打开: http://localhost/index.php?q=this%20wórds
header('Content-Type: text/html; charset=utf-8');
print_r(str_word_count($_GET['q'],1,'ó'));
,而不是返回的:
[0] this
[1] wórds
...返回:
[0] this
[1] w
[2] rds
该功能如何支持这些特殊字符t帽子是通过querystring发送的?
更新 - 它的工作就好用mario的解决方案:
function sanitize_words($string) {
preg_match_all("/\p{L}[\p{L}\p{Mn}\p{Pd}'\x{2019}]*/u",$string,$matches,PREG_PATTERN_ORDER);
return $matches[0];
}
它也很棒。第一个和第二个正则表达式有什么区别?无法让第二个人工作。 – andufo
第二个将允许像'不'这样的东西被算作单个词。完整的正则表达式当然是''/ [\ pL'] +/u''。手册http://de.php.net/manual/en/function.str-word-count.php#85592中有另一个版本,可能涵盖所有其他应该被视为单词的印刷版本。 – mario
“/ \ p {L} [\ p {L} \ p {Mn} \ p {Pd}'\ x {2019} */u”---谢谢! – andufo