Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php preg_与外部txt文件匹配_Php_Regex_Preg Match - Fatal编程技术网

Php preg_与外部txt文件匹配

Php preg_与外部txt文件匹配,php,regex,preg-match,Php,Regex,Preg Match,我有一个.txt文件,在论坛中有很多禁止使用的单词,表达方式如下: //filterwords.txt XXX YYY ZZZ 然后,我想使用preg_match来检查传入的文本$str和这些单词;如果那些被禁止的话不包括在内,我们可以做点什么;否则,我们会做另一件事。。。我不确定这个表达方式,我只知道:- $filter_word = file("filterwords.txt") for ($i=0; $i< count($filter_word);$i++) { if(!pre

我有一个.txt文件,在论坛中有很多禁止使用的单词,表达方式如下:

//filterwords.txt
XXX
YYY
ZZZ

然后,我想使用preg_match来检查传入的文本$str和这些单词;如果那些被禁止的话不包括在内,我们可以做点什么;否则,我们会做另一件事。。。我不确定这个表达方式,我只知道:-

$filter_word = file("filterwords.txt")

for ($i=0; $i< count($filter_word);$i++)
{
  if(!preg_match($filter_word[$i],$str))
  {
    echo "not ok!";
    exit;
  }
  else
  {
    echo "ok!!";
    exit;
  }
}
$filter\u word=文件(“filterwords.txt”)
对于($i=0;$i
专家们能教我如何写赛前部分吗?谢谢。

这个怎么样:

<?php
    $file = file_get_contents('filterwords.txt');
    $words = preg_split("#\r?\n#", $file, -1, PREG_SPLIT_NO_EMPTY);

    #Added to escape metacharacters as mentioned by @ridgerunner
    $words = array_filter("preg_quote", $words);

    $pattern = "#\b(". implode('|', $words) . ")\b#";

    if(preg_match($pattern, $str))
    {
        echo "bad word detected";
    }
?>

另外,假设您有文本要签入$str var

这个怎么样:

<?php
    $file = file_get_contents('filterwords.txt');
    $words = preg_split("#\r?\n#", $file, -1, PREG_SPLIT_NO_EMPTY);

    #Added to escape metacharacters as mentioned by @ridgerunner
    $words = array_filter("preg_quote", $words);

    $pattern = "#\b(". implode('|', $words) . ")\b#";

    if(preg_match($pattern, $str))
    {
        echo "bad word detected";
    }
?>


另外,假设您有文本要签入$str var

这里有很多关于亵渎过滤器的问题。例如你确定你在那里找不到任何帮助你的东西吗?你在这里发了一个帖子。如果我不知道php的问题,我能向愿意教我的人寻求帮助吗?你确定吗?这里有很多关于亵渎过滤器的问题。例如你确定你在那里找不到任何帮助你的东西吗?你在这里发了一个帖子。如果我不知道php的问题,我能向愿意教我的人寻求帮助吗?您确定吗?这几乎是一个很好的答案(如果文件大小为<
64KB
),但您确实需要通过
preg_quote()
运行单词列表以转义“单词”中可能存在的任何元字符。i、 e.插入一行:
$file=preg_quote($file,
)你说得对,但作为一个简单的例子,用普通的词来说,这可能会奏效,不过我会编辑以实现你在评论中指出的更改。如果文件更大呢?>5 MB几乎是一个很好的答案(如果文件大小为<
64KB
),但您确实需要通过
preg_quote()
运行单词列表,以转义“单词”中可能存在的任何元字符。i、 e.插入一行:
$file=preg_quote($file,
)你说得对,但作为一个简单的例子,用普通的词来说,这可能会奏效,不过我会编辑以实现你在评论中指出的更改。如果文件更大呢?>5 MB