支持特殊字符str_word_count() [英] Supporting special characters with str_word_count()

查看:100
本文介绍了支持特殊字符str_word_count()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

str_word_count()函数返回一个数组,该数组包含字符串中的所有单词.除使用特殊字符外,它的效果都很好.在这种情况下,php脚本通过querystring接收字符串:

The str_word_count() function returns an array that holds all words in a string. It works great, except when using special characters. In this case, the php script receives the string via querystring:

当我打开时: 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

此功能如何支持通过查询字符串发送的那些特殊字符?

How could this function support those special characters that are being sent through querystring?

更新-使用 mario 的解决方案就可以了:

Update - it worked out just fine by using mario's solution:

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];
}

推荐答案

不确定第三个参数是否足以使str_word_count用于非ASCII符号.它可能仅与Latin-1一起使用.

Not sure if that third parameter is sufficient to make str_word_count work for non-ASCII symbols. It probably only works with Latin-1 if anything.

但是,您也可以使用正则表达式来计算单词数:

As alternative you could count the words with a regex however:

$count = preg_match_all('/\pL+/u', $_GET['q'], $matches);

这至少适用于UTF-8.要完全复制str_word_count,您最终可能需要[\pL']+.

This works for UTF-8 at least. To fully replicate str_word_count you might need [\pL']+ eventually.

这篇关于支持特殊字符str_word_count()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆