Return to Snippet

Revision: 50426
at August 20, 2011 23:36 by paulo72


Initial Code
/////////////////////////////
// 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='' />");
}

Initial URL

                                

Initial Description
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

Initial Title
Image blacklist - test a string against a blacklist in PHP

Initial Tags
php

Initial Language
PHP