English 中文(简体)
预示着的阵列项目?
原标题:preg_match array items in string?

请允许我说,我有各种各样的坏话:

$badwords = array("one", "two", "three");

随机扼杀:

$string = "some variable text";

如何建立这一周期:

if (one or more items from the $badwords array is found in $string)
echo "sorry bad word found";
else
echo "string contains no bad words";

Example:
if $string = "one fine day" or "one fine day two of us did something", user should see sorry bad word found message.
If $string = "fine day", user should see string contains no bad words message.

如我所知,你可以从阵列上<代码>preg_match。 任何建议?

最佳回答

如何做到这一点:

$badWords = array( one ,  two ,  three );
$stringToCheck =  some stringy thing ;
// $stringToCheck =  one stringy thing ;

$noBadWordsFound = true;
foreach ($badWords as $badWord) {
  if (preg_match("/$badWord/", $stringToCheck)) {
    $noBadWordsFound = false;
    break;
  }
}
if ($noBadWordsFound) { ... } else { ... }
问题回答

Why do you want to use preg_match() here?
What about this:

foreach($badwords as $badword)
{
  if (strpos($string, $badword) !== false)
    echo "sorry bad word found";
  else
    echo "string contains no bad words";
}

如果你需要<代码>preg_match(),出于某种原因,你可以动态地产生reg形。 与此类似:

$pattern =  /(  . implode( | , $badwords) .  )/ ; // $pattern = /(one|two|three)/
$result = preg_match($pattern, $string);

HTH

如果你想通过将插手打成言语来逐字,你可以这样做:

$badwordsfound = count(array_filter(
    explode(" ",$string),
    function ($element) use ($badwords) {
        if(in_array($element,$badwords)) 
            return true; 
        }
    })) > 0;

if($badwordsfound){
   echo "Bad words found";
}else{
   echo "String clean";
}

现在,我会想到的是,如何取代阵列中的所有坏词,如果扼杀物保持不变,如何加以检查?

$badwords_replace = array_fill(0,count($badwords),"");
$string_clean = str_replace($badwords,$badwords_replace,$string);
if($string_clean == $string) {
    echo "no bad words found";
}else{
    echo "bad words found";
}

这里是坏的字过滤器,它非常有用:

private static $bad_name = array("word1", "word2", "word3");

// This will check for exact words only. so "ass" will be found and flagged 
// but not "classic"

$badFound = preg_match("/(" . implode(self::$bad_name,"|") . ")/i", $name_in);

然后,我有另一个变数,有选择性地座标:

// This will match "ass" as well as "classic" and flag it

private static $forbidden_name = array("word1", "word2", "word3");

$forbiddenFound = preg_match("/(" . implode(self::$forbidden_name,"|") . ")/i", $name_in);

接着,我操作了一个<条码>。

if ($badFound) {
   return FALSE;
} elseif ($forbiddenFound) {
   return FALSE;
} else {
   return TRUE;
}

希望这一帮助。 请让我澄清任何内容。





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签