【问题标题】:PHP replace common words from my filePHP 替换我文件中的常用词
【发布时间】:2014-03-31 08:08:37
【问题描述】:

我尝试制作一个工具,您可以在其中输入网站,当您单击提交按钮时,它会卷曲所有文本。

在所有卷曲之后,将其从标签中剥离,并计算单词。它最终是一个名为$frequency 的数组。如果我使用<pre> 标签回显它,它将向我展示一切都很好! (注意:我将内容放在一个文件中,$homepage = file_get_contents($file);,这是我在代码中使用的,我不知道这是否重要)

但是我并不关心 or 这个词是否在网站上出现 200 次,我只想要重要的词。所以我做了一个包含所有常用词的数组。最终在 $common_words 变量中设置。但我似乎无法找到一种方法来替换在$frequency 中找到的所有单词,如果它们也在$common_words 中找到,则用"" 替换它们。

经过一番研究,我发现了这段代码:

$string = 'sand band or nor and where whereabouts foo';
$wordlist = array("or", "and", "where");

foreach ($wordlist as &$word) {
    $word = '/\b' . preg_quote($word, '/') . '\b/';
}

$string = preg_replace($wordlist, '', $string);
var_dump($string);

如果我复制粘贴它,它可以正常工作,从字符串中删除 or, and, where。 但是用$frequency 替换$string 或用$common_words 替换$wordlist 要么不起作用,要么给我一个错误,例如:Delimiter must not be alphanumeric or backslash

如果没有,我希望我已经正确地提出了我的问题。请告诉我!

提前致谢

编辑:好的,我已经缩小了很多范围。首先,我忘记了foreach ($wordlist as &$word) { 中的&

但由于它正在计算所有的单词,它所替换的单词仍然被计算在内。请看这 2 个屏幕截图以了解我的意思:http://imgur.com/oqqZR3h,xHEZKRz#0

【问题讨论】:

  • 我不确定你的问题是什么,但你的正则表达式是如何工作的?它不应该像 /or|and|where/ 吗?好久没做PHP了,不习惯这么粗糙的API,但是从这里看好像不是你在做的。
  • @Michael 最后的一段代码是我在互联网上找到的,如果我复制粘贴并运行这段代码,它工作正常,它会删除单词或,以及从哪里细绳。但是用我自己的变量替换 $string 和 $wordlist 不起作用
  • 它对我有用,即使我更改了单词表。我真的不明白你有什么问题?这一切都很好!

标签: php arrays


【解决方案1】:

如果我理解正确的话,你不会因为忽略所谓的常用词而知道每个词出现了多少次。

假设 $url 是您将要运行的页面,$common_words 是您的常用词数组,您可以执行以下操作:

// Get the page content's and strip the html tags
$contents = strip_tags( file_get_contents($url) );

// This will split the words from the contents, creating an array with each word in it
preg_match_all("/([\w]+[']?[\w]*)\W/", $contents, $words);

$common_words = array('or', 'and', 'I', 'where');

$frequency = array();

// Count occurrences
$frequency = array_count_values($words[0]);
unset($words); // Release all that memory

var_dump($frequency);

此时,您将拥有一个关联数组,其中包含每个不常见的单词和一个显示给定单词出现次数的计数。

更新

更多关于 RegEx 的信息。我们需要匹配单词。最简单的方法是:(\w+)。但这与I'vehaven't 之类的词不匹配(注意')。这就是我让它变得更复杂的目的。此外,\w 不支持在 6-year-old 中使用破折号。

所以我创建了一个子组,它应该匹配单词中的字符,包括虚线和单引号。

(?:\w'|\w|-)

开头的?: 部分是do not matchdo not include in the results。那是因为我所做的只是对单词内容的选项进行分组。为了匹配整个单词,RegEx 将匹配上面的一个或多个子组:

((?:\w'\w|\w|-)+)

所以 RegEx preg_match_all() 行应该是:

preg_match_all("/((?:\w'\w|\w|-)+)/", $contents, $words);

希望这会有所帮助。

【讨论】:

  • 谢谢,它对我来说就像 50%。我将尝试使用它,直到它适合我现有的代码。忍受我,如果我能让它工作,我会接受你的回答=/
  • 好吧,我现在很迷茫。我无法将我拥有的代码和您的代码放在一起。您提供的代码有效。但不知何故,在我的其他代码中,它为我提供了好词,并且使用您的代码,它给了我一些词,例如:hello.good
  • 如果你有兴趣,这是我之前的代码,pastie.org/private/f5imeqpzi6oz7vuvstdxw
  • @DejaVu 看看我的答案的更新版本。
  • 我正在针对 I am only in this position 'cuz of my 6-year-old little brother who wouldn't listen to me. 对其进行测试,我认为它涵盖了我提出的大多数极端情况。
【解决方案2】:

我已将 $wordlist 更改为 $mywordlist。仍然有效!

<?php
$string = 'sand band or nor and where whereabouts foo';
$wordlist = array("or", "and", "where");
$mywordlist=array("sand","band");
foreach ($mywordlist as &$word) {
    $word = '/\b' . preg_quote($word, '/') . '\b/';
}

$string = preg_replace($mywordlist, '', $string);
var_dump($string);
?>

【讨论】:

    【解决方案3】:

    我想你可以这样做:

    $common_words = "foo baq etc etc";
    
    $str = "foo bar baz"; // input
    
    foreach (explode(" ", $common_words) as $word){
       $str = strtr($str, $word, "");
    }
    

    【讨论】:

      猜你喜欢
      • 2015-03-02
      • 2018-02-09
      • 1970-01-01
      • 2019-04-10
      • 2014-04-15
      • 2016-10-19
      • 2020-01-06
      • 1970-01-01
      相关资源
      最近更新 更多