检查文本是否包含PHP中txt文件中的单词

时间:2014-12-23 23:36:45

标签: php arrays text

我的目标是检查我在文本框中输入的内容是否与我列表中的任何单词匹配。我的列表位于.txt文件中。我想我应该将.txt转换为数组,并将其值与另一个数组进行比较,该数组来自文本框(表单)。我假设我应该将.txt文件中的文本带到一个数组中,但这种比较并不是很有效。

可能是这样的:

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

谢谢!节日快乐!

3 个答案:

答案 0 :(得分:0)

您可以在线进行或从页面复制代码

http://www.wordcounter.net/

答案 1 :(得分:0)

你是如何进行比较的? 您可以将单词放在数组中,然后与in_array()

进行比较

答案 2 :(得分:0)

首先使用file_get_contents()后跟explode()preg_match_all()将单词列表作为数组加载。然后检查消息中的每个单词是否在列表中,反之亦然。您可以使用strpos()查找邮件中的每个单词,这将找到" thorpe"如果" Scunthorpe"在消息中。或者您也可以将消息分解为单词并查找列表中的每个单词,这将忽略虚假子串。以下命令行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