SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard mPeror's Avatar
    Join Date
    Mar 2005
    Location
    Saudi Arabia
    Posts
    1,724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation How do I get the 'src' of 'img' tags?

    Hi,

    I basically have html <img> tags that I'd like to get the "src" of. The <img> tags that I have contain a few other attributes as well, and the order of the attributes is different from one <img> to another.

    I'm thinking this should be done in regex, but I have no idea how to do it.

    I've found a few solutions like this one, but none of them actually worked for me for some reason.


    Any idea?

  2. #2
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mPeror View Post
    Hi,

    I basically have html <img> tags that I'd like to get the "src" of. The <img> tags that I have contain a few other attributes as well, and the order of the attributes is different from one <img> to another.

    I'm thinking this should be done in regex, but I have no idea how to do it.

    I've found a few solutions like this one, but none of them actually worked for me for some reason.


    Any idea?

    Instead of reg ex I would suggest looking into the DomDocument class for this:
    http://us3.php.net/manual/en/class.domdocument.php
    http://us3.php.net/manual/en/book.dom.php
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $sHTML 
    '
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title></title>
        </head>
        <body>
            <img src="foo" />
            <img src="bar" />
            <img src="foo" />
            <img src="bar" />
            <img src="foo" />
            <img src="bar" />        
        </body>
    </html>
    '
    ;
    $oXPath = new DOMXPath(DOMDocument::loadHTML($sHTML));
    foreach (
    $oXPath->query("//img") as $oNode)
    {
        echo 
    $oNode->getAttribute('src');
    }
    /*
        foo
        bar
        foo
        bar
        foo
        bar
    */
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Wizard mPeror's Avatar
    Join Date
    Mar 2005
    Location
    Saudi Arabia
    Posts
    1,724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oooo these are pretty interesting classes. I never knew about them.

    Thanks guys. SilverBulletUK's solution did it for me.

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    For the (very rare) occasion where there might be an image with no src attribute (I guess you can never count on the reliability of someone else's markup!), SilverBulletUK's code will trip up. For that reason, but more just to show you it's possible, you may instead query directly for src attributes of img elements with XPath.

    PHP Code:
    foreach ($oXPath->query("//img/@src") as $oAttr)
    {
        echo 
    $oAttr->value "\n";

    Salathe
    Software Developer and PHP Manual Author.

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    For the (very rare) occasion where there might be an image with no src attribute (I guess you can never count on the reliability of someone else's markup!), SilverBulletUK's code will trip up. For that reason, but more just to show you it's possible, you may instead query directly for src attributes of img elements with XPath.

    PHP Code:
    foreach ($oXPath->query("//img/@src") as $oAttr)
    {
        echo 
    $oAttr->value "\n";

    Thanks Peter, y'know, I really do enjoy your re-factorings of my code. Seriously, just when I think I've nailed it....

    Here's to many more.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •