- Code: Select all
<?php
function getBlacklist(){
$result = array();
$handle = fopen("blacklist.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$result[] =$buffer;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
return $result;
}
function contains_bad_words($text){
$result=false;
foreach (getBlacklist() as $banned_word){
$pos = strpos($text, $banned_word);
if ($pos !== false) {
echo "The string '$banned_word' was found in the string '$text'";
echo " and exists at position $pos <br />";
$result = true;
break;
}
}
return $result;
}
$text="this is an example 4u.";
if(contains_bad_words($text))
echo "String contains bad words";
else
echo "String is fine";
?>
-- Tue Apr 24, 2012 11:19 pm --
ok, figured it out.
It appears there's a space behind every word in the array i create. A simple trim solved it

