retrieve only images without a certain id.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lehula
Forum Newbie
Posts: 7
Joined: Sat Jul 14, 2007 4:33 pm

retrieve only images without a certain id.

Post by lehula »

I'm retrieving all images in a section of my source code. I want it to skip all the images that have the id="blocker". I've tried using strpos to search for the id in the image src. I then try to skip the image with that id by using the 'continue' expression, but it's not working. Can someone help me out? Thanks.

Code: Select all

//retrieve all images within the above string
preg_match_all('#<img\s[^>]*src\s*=\s*[\'"]?([^\'"\s>]+)[\'"]?[\s>]#i', $matches[0], $images);
foreach($images[1] as $image)
{
$ider = 'id="blocker"';
$pos = strpos($image, $ider);


if ($pos === true) {
continue;} else {

echo "document.getElementById('randompic".$h."').src=\"".$image."\";";
echo "document.getElementById(\"picaddress".$h."\").value = \"".$image."\";";
echo "document.getElementById('randompic".$h."').style.visibility = \"visible\";";
echo "document.getElementById(\"randomcheck".$h."\").checked = \"true\";";
echo "document.getElementById('randompreview".$h."').src=\"".$image."\";";
$h++;}
}
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Are you sure that your regex even properly catches the image tags with ids? Looks iffy.

And you could always turn the if...else into a simple if.

Code: Select all

if($pos === false)
{
   ...
}
Post Reply