Jump to content

[SOLVED] Removing small/invalid images


Valleriani

Recommended Posts

Hello, I've been trying to remove small or invalid images with a width of 1. Right now this is my example script, we will pretend $CONTENT is this:

 

$content = 'Hello welcome to my site, you are visitor 300. <img src=http://www.blah.com/image1.jpg width="500" height="500"> So how are you? <img src=http://www.blah.com/image221.jpg width="1" height="1">.. I am fine! <img src=http://www.blah.com/image23.jpg width="1" height="1"> Thats good!'

 

As you see, the SECOND and THIRD image would be removed because it has the width of 1, which I want to remove. The first one would be fine... Here is the script so far.. Right now it just produces invalid things..

 

$pcount = substr_count($content, '<img');
if ( $pcount >= 1 ) {
for ( $counter = 1; $counter <= $pcount; $counter += 1) {
	$pos = strpos($content, '<img', $pos3);
	if ($pos === false) {
	} else {
	$pos2 = strpos($content, '>', $pos);
	$pos3 = $pos2 + 3;
		if ($pos2 === false) {
		} else {
			$tostrip = substr_replace($content, '', 0, $pos - 1);
			$pos4 = strpos($tostrip, '>');

			$tostrip = substr_replace($tostrip, '', $pos4 + 2);
			$pcount2 = substr_count($tostrip, 'width="1"');

			if ($pcount2 >= 1 ) {
				$content = substr_replace($content , '', $pos, $pos2 + 1);
			}
		}
	}
}
}
echo $content;

 

Any suggestions? Thanks!

 

Edit: The end results should display:

 

$content = 'Hello welcome to my site, you are visitor 300. <img src=http://www.blah.com/image1.jpg width="500" height="500"> So how are you? .. I am fine! Thats good!'

Link to comment
https://forums.phpfreaks.com/topic/50108-solved-removing-smallinvalid-images/
Share on other sites

try

<?php
$content = 'Hello welcome to my site, you are visitor 300. <img src=http://www.blah.com/image1.jpg width="500" height="500"> So how are you? <img src=http://www.blah.com/image221.jpg width="1" height="1">.. I am fine! <img src=http://www.blah.com/image23.jpg width="1" height="1"> Thats good!';

$p1 = $p2 = 0;
$k = strlen($content);
$result = '';
while ($p1 < $k) {
    $p1 = strpos($content, '<img', $p2);
    if ($p1===false) $p1 = $k;
    $result .= substr($content,$p2, $p1-$p2);
    $p2 = strpos($content, '>', $p1)+1;
    $image = substr($content,$p1, $p2-$p1);
    if (!strpos($image, 'width="1"')) {
        $result .= $image ;
    }
}
echo $result;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.