Php 使用特殊字符获取最常用的单词
我想从数组中获取最常用的单词。唯一的问题是瑞典语字符(Å、Ä和Ö)只能显示为�.Php 使用特殊字符获取最常用的单词,php,arrays,count,words,Php,Arrays,Count,Words,我想从数组中获取最常用的单词。唯一的问题是瑞典语字符(Å、Ä和Ö)只能显示为�. $string = 'This is just a test post with the Swedish characters Å, Ä, and Ö. Also as lower cased characters: å, ä, and ö.'; echo '<pre>'; print_r(array_count_values(str_word_count($string, 1, 'àáãâçêéíîó
$string = 'This is just a test post with the Swedish characters Å, Ä, and Ö. Also as lower cased characters: å, ä, and ö.';
echo '<pre>';
print_r(array_count_values(str_word_count($string, 1, 'àáãâçêéíîóõôúÀÁÃÂÇÊÉÍÎÓÕÔÚ')));
echo '</pre>';
如何才能“看到”瑞典语字符和其他特殊字符?我成功地删除了� 通过将
添加到中进行签名,所有这些都是在假设您正在使用UTF-8的情况下运行的
您可以采用一种简单的方法,使用在任何分隔符、标点符号或控制字符上拆分字符串
preg_split
示例:
输出:
这对于给定的字符串很好,但不一定以区域设置感知的方式拆分单词。例如,诸如“is-not”这样的收缩将被分解为“is”和“t”
谢天谢地,在PHP7中添加了大量处理此类事情的功能
该计划将是:
- *使用的输入可确保所有图形均以一致的方式编码。例如,
ä
可以通过两种方式进行编码和计数:
- U+00E4“带分音符的拉丁文小写字母A”或
- U+0061'拉丁文小写字母A'后接U+0308'组合分音符'
- 通过获取以与区域设置相关的方式打断单词的。这就理解了给定区域的“单词”是由什么组成的,包括处理诸如“不是”之类的收缩
- 获取其via,以便在文本片段上进行迭代
- 跳过你不关心的事情
(*请注意,无论使用何种方法分解字符串,您都可能希望执行规范化-最好在上面的preg_split
或您决定使用的任何方法之前执行。)
国际示例:
输出:
这更为详细,但如果您希望(支持Intl扩展的库)在理解单词的组成时承担繁重的工作,那么这可能是值得的。这里有一个解决方案,使用正则表达式使用Unicode标点分割“单词”,然后只进行常规数组出现计数
Array
(
[This] => 1
[is] => 1
[just] => 1
[a] => 1
[test] => 1
[post] => 1
[with] => 1
[the] => 1
[Swedish] => 1
[characters] => 2
[Å] => 1
[Ä] => 1
[and] => 2
[Ö] => 1
[Also] => 1
[as] => 1
[lower] => 1
[cased] => 1
[å] => 1
[ä] => 1
[ö] => 1
)
产生:
这是在unicode控制台中测试的,如果您使用的是浏览器,则可能需要使用编码。在浏览器中创建一个
标记或设置编码,或发送PHP标题。如果任何名称以str
开头的PHP函数不具有多字节安全性,您应该不会感到惊讶。用户在手册中的评论建议了替代方法。@CBroe…名称以str开头的PHP函数。
此函数在哪里?请尝试此函数mb\u str\u word\u count
而不是str\u word\u count
:@CatalinB谢谢,但输出将是这样的:数组([这只是一篇带有瑞典文字的测试文章�, �, 和Ö。也作为小写字符:�, �, 及�.] => 1)
非常感谢您提供非常详细的答案。您的答案和MarZab的答案都非常好。您的正则表达式将接受smileys,而MarZab的正则表达式将不接受。如果可以,我将接受两个答案,但由于MarZab的正则表达式不接受smileys,因此我将接受他的答案。
$split = preg_split('/[\pZ\pP\pC]/u', $string, -1, PREG_SPLIT_NO_EMPTY);
print_r(array_count_values($split));
Array
(
[This] => 1
[is] => 1
[just] => 1
[a] => 1
[test] => 1
[post] => 1
[with] => 1
[the] => 1
[Swedish] => 1
[characters] => 2
[Å] => 1
[Ä] => 1
[and] => 2
[Ö] => 1
[Also] => 1
[as] => 1
[lower] => 1
[cased] => 1
[å] => 1
[ä] => 1
[ö] => 1
)
$string = Normalizer::normalize($string);
$iter = IntlBreakIterator::createWordInstance("sv_SE");
$iter->setText($string);
$words = $iter->getPartsIterator();
$split = [];
foreach ($words as $word) {
// skip text fragments consisting only of a space or punctuation character
if (IntlChar::isspace($word) || IntlChar::ispunct($word)) {
continue;
}
$split[] = $word;
}
print_r(array_count_values($split));
Array
(
[This] => 1
[is] => 1
[just] => 1
[a] => 1
[test] => 1
[post] => 1
[with] => 1
[the] => 1
[Swedish] => 1
[characters] => 2
[Å] => 1
[Ä] => 1
[and] => 2
[Ö] => 1
[Also] => 1
[as] => 1
[lower] => 1
[cased] => 1
[å] => 1
[ä] => 1
[ö] => 1
)
array_count_values(preg_split('/[[:punct:]\s]+/u', $string, -1, PREG_SPLIT_NO_EMPTY));
Array
(
[This] => 1
[is] => 1
[just] => 1
[a] => 1
[test] => 1
[post] => 1
[with] => 1
[the] => 1
[Swedish] => 1
[characters] => 2
[Å] => 1
[Ä] => 1
[and] => 2
[Ö] => 1
[Also] => 1
[as] => 1
[lower] => 1
[cased] => 1
[å] => 1
[ä] => 1
[ö] => 1
)