views:

27

answers:

2

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 />';
}
A: 

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 />';
    }
}
Pascal MARTIN
Thanks for the reply, I will give that a shot now. I think I may have solved the image tag issue by using the following code:foreach($images as $image) { echo '<img src='. $image->getAttribute('src') . 'width="142" height="106" alt="photo"'. '<br />';}I will see how it fits together now.
Jeremy
Figured it out, it all works perfectly now. Thanks heaps!
Jeremy
Oh, yes, sorry, I didn't see that part of the question :-( ;; still, glad you found out ;-)
Pascal MARTIN
A: 

Simple:

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";
    }
}
Alix Axel