Tutorial :echo image url with tags in php


I have previously asked a question on how to echo the url of images from an html page. I can do this successfully but how can I narrow this down further so only images urls beginning with a certain phrase are shown, furthermore how can I add an image tag around them so the images are displayed as images and not just text?

e.g I only want to list images beginning with http://photos.website.com.

edit: I forgot to mention this is the code used to iterate through the images:

foreach($images as $image) {      echo $image->getAttribute('src') . '<br />';  }  


You will have to add a condition that tests the content of $image->getAttribute('src').

To test if a string beings by another, a possibility is to use the strpos function, which returns the position of the needle in the haystack -- here, you want that position to be 0 (i.e. the first character of the string).

foreach($images as $image) {      $url = $image->getAttribute('src');      if (strpos($url, 'http://photos.website.com') === 0) {          echo $url . '<br />';      }  }  



foreach ($images as $image) {      $src = $image->getAttribute('src');        if (stripos($src, 'http://photos.website.com') === 0)      {          echo $src . '<br />';      }  }  

And to add tags:

foreach ($images as $image) {      $src = $image->getAttribute('src');        if (stripos($src, 'http://photos.website.com') === 0)      {          echo sprintf('<img src="%s" alt="" />', $src) . "\n";      }  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »