I am doing a site for publishing ads, in a section of adding the campaign (ads) I need to prevent certain badwords from the campaign title. So I have checked those title when the advertiser adds the campaign. These badwords are stored in a file in txt format. I have used this code.
$string = $description;
$badwords = file( "badwords.txt" );
if ( $badwords ) {
foreach ( $badwords as $line ) {
if ( preg_match("/$line/", $string) ) {
echo $string; die();
return array ( 0, $language['bad_words_error'] );
}
}
}
I have tried in_array() but i can’t match the content in $string.
if ( in_array($string, $badwords) ) {
echo "The word ".$string ." is a bad keyword";
}
Usually, the problem comes from trailing spaces left in the file.
To solve this, use
trim()on individual$lines.