I have a PHP function to check if a string contains specific (full) 'words' from an array (some of these 'words' may start with a special character followed by a space OR end with a space). The problem is with 'words' that start with special characters, for example: +, -, /, $, # etc. Why this 'contains' function doesn't catch such words? I added preg_quote to it and it still doesn't work.
$bads = array('+11'," - 68",'[img','$cool ', "# hash");
// disallowed full 'words';**some may start with a special character + space or end with a space**; if one of them appears in string, the function should return true
$s= 'This is +11 test to show if $cool or [img works but it does $cool not';
//another example to test: $s= 'This - 68 is # hash not';
if(contains($s,$bads)) {
echo 'Contains! ';
}
#### FUNCTION ###
function contains($str, $bads)
{
foreach($bads as $a) {
$a=preg_quote($a,'/');
if(preg_match("/\b".$a."\b/",$str)) return true;
}
return false;
}