Image blacklist – test a string against a blacklist in PHP
/////////////////////////////
// Function
// compare string to a blacklist of image url fragments to avoid
function imageBlacklist($str){
// words to filter
$badwords = array( "//feeds.", "b.gif", "//api." );
$result_of_filter = 0;
foreach ($badwords as $value) {
//echo($value);
$find_pos = strpos($str,$value);
if ($find_pos > 0) {
$result_of_filter = 'blacklisted';
break;
}
}
unset($value); // break the reference with the last element
return $result_of_filter;
}
/////////////////////////////
// usage
// $test_this contains an example string — an image URL
$test_this = "http://example.com/images.b.gif";
// invoke the function to check our example string
$test_result = imageBlacklist($test_this);
// if the function returns blacklist we know a match was found
if ($test === 'blacklisted') {
// use the fall back image url instead
echo("<img src='http://example.com/backup-image.png' alt='' />");
} else {
// the url is not on black list so use for our image
echo("<img src='".$test_this."' alt='' />");
}

Processing your request, Please wait....
No Responses to “Image blacklist – test a string against a blacklist in PHP”
Comments (Your Comments)
Leave a Reply