Ichier » coding » ichier's coding corner
{ php }  
  mstrstr
date 2004
update 2006
code <?php
function mstrstr($string,$array) {            //coded by Ichier2004
    
foreach($array as $str) {
        if(
is_array($str)) { 
            foreach(
$str as $st) {
                if(!
strstr($string,$st)) { break 2; }
            }
            return 
true;
        } else {
            if(
strstr($string,$str)) { return true; }
        }
    }
    return 
false;
}

function 
mstristr($string,$array) {            //coded by Ichier2004
    
foreach($array as $str) {
        if(
is_array($str)) { 
            foreach(
$str as $st) {
                if(!
stristr($string,$st)) { break 2; }
            }
            return 
true;
        } else {
            if(
stristr($string,$str)) { return true; }
        }
    }
    return 
false;
}
?>
description searches in a string for a collection of stings from an array, returns true or false
you can also search for more than one words in combination
mstristr is case-insensitive
syntax <?php
bool 
mstristr(string haystack, array needles);
?>
explanation so we search for a list of badwords in a text
<?php
    $badwords
[0] = "viagra";
    
$badwords[1] = "cialis";
    
$badwords[2] = "casino";
    
$badwords[3] = "porn";

    
$text "Hi Your website is sooo fantastic! Please Try my viagra!";
    
    
$bool mstristr($text,$badwords);
?>

in that case $bool is true.

ok, now we want all badwords as combination, so a badword has to occure with a link
<?php
    $badwords
[0] = array("http:","viagra");
    
$badwords[1] = array("http:","cialis");
    
$badwords[2] = array("http:","casino");
    
$badwords[3] = array("http:","porn");

    
$text "Hi Your website is sooo fantastic! Please Try my viagra or cialis!";
    
    
$bool mstristr($text,$badwords);
?>

in that case $bool is false, cause none of the combinations was found.