Php 如何检查字符串中是否有坏单词

Php 如何检查字符串中是否有坏单词,php,Php,这是我的PHP代码,用于将字符串中的坏单词替换为*,运行良好 但是我想检查字符串中是否有坏单词。我该怎么做 index.php <?php include("badwords.php"); $content = "this is an example string with bad words in it"; $badword = new badword(); echo $badword->word_filter("$content"); if($badword->usedBa

这是我的PHP代码,用于将字符串中的坏单词替换为*,运行良好

但是我想检查字符串中是否有坏单词。我该怎么做

index.php

<?php
include("badwords.php");
$content = "this is an example string with bad words in it";
$badword = new badword();
echo $badword->word_filter("$content");
if($badword->usedBadWords()){
    // do whatever you want to do if bad words were used
}
?>
<?php
include("badwords.php");
$content = "cat bad_word dog";
$badword = new badword();
$return_val = $badword->word_fliter("$content");
echo $return_val['content'];
if($return_val['has_bad_words']){
  ..do stuff
}
?>

badword.php

<?php
$bad_words = array (
   // an array of bad words here...
);

class badword {
    function word_fliter($content) {
        global $bad_words, $wordreplace;
        $count = count($bad_words);
        $has_bad_words = false;
        for ($n = 0; $n < $count; ++$n, next ($bad_words)) {
            $filter = "*";

            //Search for bad_words in content
            $search = "$bad_words[$n]";
            $content = preg_replace("'$search'i","<i>$filter</i>",$content);
            if(preg_match("'$search'i", $content) && !$has_bad_words){
               $has_bad_words = true;
            }
            else {
               $has_bad_words = false;
            }
        }
        return array('content' => $content, 'has_bad_words' => $has_bad_words);
    }
}

编辑:因为您需要完整的代码。。。
请注意,我将函数名从word_fliter()更改为word_filter()

index.php

<?php
include("badwords.php");
$content = "this is an example string with bad words in it";
$badword = new badword();
echo $badword->word_filter("$content");
if($badword->usedBadWords()){
    // do whatever you want to do if bad words were used
}
?>
<?php
include("badwords.php");
$content = "cat bad_word dog";
$badword = new badword();
$return_val = $badword->word_fliter("$content");
echo $return_val['content'];
if($return_val['has_bad_words']){
  ..do stuff
}
?>

badwords.php

<?php
$bad_words = array (
    // insert your naughty list here
    );

class badword {
    private $usedBadWords = false;

    function word_filter($content) {
        foreach($bad_words as $bad_word){
            if(strpos($content, $bad_word) !== false){
                $this->usedBadwords = true;
            }
            $content = str_replace($bad_word, '***', $content);
        }
        return $content;
    }

    function usedBadWords(){
        return $this->usedBadWords;
    }
}
?>

这应该行得通,您只需检查
内容是否匹配任何不好的单词并返回即可

index.php

<?php
include("badwords.php");
$content = "this is an example string with bad words in it";
$badword = new badword();
echo $badword->word_filter("$content");
if($badword->usedBadWords()){
    // do whatever you want to do if bad words were used
}
?>
<?php
include("badwords.php");
$content = "cat bad_word dog";
$badword = new badword();
$return_val = $badword->word_fliter("$content");
echo $return_val['content'];
if($return_val['has_bad_words']){
  ..do stuff
}
?>

badword.php

<?php
$bad_words = array (
   // an array of bad words here...
);

class badword {
    function word_fliter($content) {
        global $bad_words, $wordreplace;
        $count = count($bad_words);
        $has_bad_words = false;
        for ($n = 0; $n < $count; ++$n, next ($bad_words)) {
            $filter = "*";

            //Search for bad_words in content
            $search = "$bad_words[$n]";
            $content = preg_replace("'$search'i","<i>$filter</i>",$content);
            if(preg_match("'$search'i", $content) && !$has_bad_words){
               $has_bad_words = true;
            }
            else {
               $has_bad_words = false;
            }
        }
        return array('content' => $content, 'has_bad_words' => $has_bad_words);
    }
}



您希望输出效果如何?您自己做了哪些尝试?试着在你的问题上更彻底一些,告诉我们你已经做了什么。我想检测我的旧字符串是否有坏单词?那么这段代码有什么问题?似乎有效。您可能需要使用单词边界,这样玻璃就不会被错误地剥离。是的,这很好。但我想知道我的odl字符串是否有坏单词?@mongkonPea最简单的方法是将原始字符串与过滤后的字符串进行比较。尽管你应该反思一下这些词的用法,因为你甚至不可能希望过滤掉所有的不好的词,并且在某个时候会无意中过滤掉一些不坏的词。例如,“pussy”是称呼猫和“bitch”为母狗的一个非常常见的术语,将“gay”定义为一个坏词会让整个社区都非常不喜欢你。如果没有
foreach
loop:
$content=str\u replace($bad\u words,***',$content)
.TIL str_替换的可以将数组作为参数:)使$usedBadwords成为类badword中的私有变量。将for循环替换为my foreach循环(第二个示例)。在badword类中创建第二个名为usedBadWords或类似的函数,在其中返回变量$usedBadWords。在索引文件中,您可以调用新函数来检查是否有任何坏单词。(此调用必须在调用筛选器()之后进行)function.T\u T你能更新你的答案吗?我不明白不,如果
preg\u match
返回1/true,那么你知道
preg\u replace
被触发了。你是什么意思?好吧,这本身就说明了问题,但这里它是一个坏的单词数组?它没有得到任何东西,你以前没有测试过吗?这是你从另一个答案得到的解决方案吗?如果是,y你应该接受你从那里得到的。这没有意义……我要回家了xD@Cashbee我一点也不怪你。