/ Published in: PHP
I was working on a site that pulled in and republished a bunch of RSS feeds. Some contained duff images such as blank gifs that I needed to block so I wrote this function to compare the url of an image against a blacklist
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
///////////////////////////// // Function // compare string to a blacklist of image url fragments to avoid function imageBlacklist($str){ // words to filter $result_of_filter = 0; foreach ($badwords as $value) { //echo($value); if ($find_pos > 0) { $result_of_filter = 'blacklisted'; break; } } 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='' />"); }