【问题标题】:Checking if a text contains words from a txt file in PHP检查文本是否包含 PHP 中 txt 文件中的单词
【发布时间】:2014-12-23 23:36:44
【问题描述】:

我的目标是检查我在文本框中输入的任何内容是否与列表中的任何单词匹配。我的清单在.txt 文件中。我想我应该将.txt 转换为一个数组,并可能将其值与另一个数组进行比较,该数组来自文本框(表单)。我以为我应该将 .txt 文件中的文本放入一个数组中,但这种比较不太奏效。

可能是这样的:

$count = 0;
If ($textbox contains $anyofthewordsfromthe.txt file)
 echo "Name of the words:" $numberofocurrences.
Else
  echo "No words from the list!"

谢谢!节日快乐!

【问题讨论】:

  • 你的文本文件有多少字???
  • 谷歌你的问题的标题。你一定会找到一些东西,我 100% 确定。
  • 你试过什么?向我们展示一些您尝试过但无法正常工作的代码。

标签: php arrays text


【解决方案1】:

你可以在网上做或从页面复制代码

http://www.wordcounter.net/

【讨论】:

    【解决方案2】:

    你是如何进行比较的? 你可以把单词放在一个数组中,然后用 in_array() 进行比较

    【讨论】:

      【解决方案3】:

      首先将单词列表加载为数组,使用file_get_contents(),后跟explode()preg_match_all()。然后检查消息中的每个单词是否在列表中,反之亦然。您可以使用strpos() 查找消息中的每个单词,如果消息中有“Scunthorpe”,它将找到“thorpe”。或者您也可以将消息分解为单词并在列表中查找每个单词,这将忽略虚假子字符串。以下命令行 PHP 脚本显示了这两种方法:

      <?php
      
      // Like explode() but uses any sequence of spaces as delimiter.
      // Equivalent to Python s.split()
      function explode_space($s) {
        preg_match_all('/[^\s]+/', $s, $words);
        return $words[0];
      }
      
      $swears_filename = 'words.txt';
      
      // Load all words from the file
      $swears = strtolower(file_get_contents($swears_filename));
      $swears = explode_space($swears);
      
      // In a web environment, it'd probably be more like this:
      // $naughty_text = trim(@$_POST['comment']);
      $naughty_text = 'I tweeted about passing the third rep milestone on Stack Overflow.';
      
      // Perform case-insensitive comparison by lowercasing everything first.
      $naughty_text = strtolower($naughty_text);
      
      // There are two solutions. The first uses substring matching,
      // which finds "thorpe" in "Scunthorpe" if "thorpe" is in words.txt.
      foreach ($swears as $swear) {
        if (strpos($naughty_text, $swear) !== false) {
          echo "Text contains substring $swear\n";
        }
      }
      
      // The other solution will find "Scunthorpe" only if "scunthorpe"
      // itself is in words.txt because it checks the whole word.
      // First convert the list of values to a set of keys to speed up
      // testing whether each word is in the set because
      // array_key_exists($k, $array), which looks for keys, is
      // faster than in_array($v, $array), which looks for values.
      $swears = array_fill_keys($swears, true);
      
      // Now convert the post to a list of distinct words.
      $naughty_text = explode_space($naughty_text);
      
      foreach ($naughty_text as $word) {
        if (array_key_exists($word, $swears)) {
          echo "Text contains word $word\n";
        }
      }
      

      当我运行这个时(向已故的乔治卡林道歉):

      $ cat words.txt
      slit pass puck cult locksacker monkeyfighter hits part third tweet
      $ php so27629576.php
      Text contains substring pass
      Text contains substring third
      Text contains substring tweet
      Text contains word third
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-24
        • 2011-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多