Recently I'm working on bad content(such as advertise post) filter of a BBS.And I write a functio开发者_StackOverflown to detect a string is in another string not continuously.Code as below:
$str = 'helloguys';
$substr1 = 'hlu';
$substr2 = 'elf';
function detect($a,$b) //function that detect a in b
{
$c = '';
for($i=0;$i<=strlen($a);$i++)
{
for($j=0;$j<=strlen($b);$j++)
{
if($a[$i] == $b[$j])
{
$b=substr($b,$j+1);
$c .=$a[$i];
break;
}
}
}
if($c == $a) return true;
else return false;
}
var_dump(detect($substr1,$str)); //true
var_dump(detect($substr2,$str)); //false
Since the filter works before the users do their posts so I think the efficiency here is important.And I wonder if there's any better solution? Thanks!
a faster way to do this is converting $a to a regular expression and match it with $b, so that you just leave the optimization to the PCRE module itself which is written in C code.
for example:
detect("hlu",$b) is equal to preg_match("/h.*l.*u/", $b)
(detect("hlu",$b) && detect("elf",$b)) is equal to preg_match("/(h.*l.*u|e.*l.*f)/", $b)
not sure why you would want to do this. but i was bored
function detect( $a,$b ) {
return count( array_intersect( str_split($b), str_split($a) ) ) == strlen($b);
}
精彩评论