SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2007
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular Expression help or similar?

    Hi All,

    I'm using flickr and their API to call out images from my flickr account.

    Now the typical output I get from flickr is like so:

    Code:
    <p><a href="http://www.flickr.com/people/horshamrebel/">Horsham Rebel</a> posted a photo:</p>
    <p><a href="http://www.flickr.com/photos/horshamrebel/4040847816/" title="Slough Town v Stourport Swifts"><img src="http://farm4.static.flickr.com/3531/4040847816_0d926a6f32_m.jpg" width="240" height="145" alt="Slough Town v Stourport Swifts" /></a></p>
    
    <p>Through the goal netting at the players warming up</p>
    However This is the only part of the code I want outputted

    Code:
    <a href="http://www.flickr.com/photos/horshamrebel/4040847816/" title="Slough Town v Stourport Swifts"><img src="http://farm4.static.flickr.com/3531/4040847816_0d926a6f32_m.jpg" width="240" height="145" alt="Slough Town v Stourport Swifts" /></a>
    So is there something I can do in php to say only grab the <a href tag> that wraps around the <img src> code and ditch the rest?

  2. #2
    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
    $string 
    '
    <p><a href="http://www.flickr.com/people/horshamrebel/">Horsham Rebel</a> posted a photo:</p>
    <p><a href="http://www.flickr.com/photos/horshamrebel/4040847816/" title="Slough Town v Stourport Swifts"><img src="http://farm4.static.flickr.com/3531/4040847816_0d926a6f32_m.jpg" width="240" height="145" alt="Slough Town v Stourport Swifts" /></a></p>
    <p>Through the goal netting at the players warming up</p>
    '
    ;

    $obj = new SimpleXMLElement(sprintf('<root>%s</root>'$string));
    echo 
    array_shift($obj->xpath('//img/..'))->asXML();
    /*
        <a href="http://www.flickr.com/photos/horshamrebel/4040847816/" title="Slough Town v Stourport Swifts">
            <img src="http://farm4.static.flickr.com/3531/4040847816_0d926a6f32_m.jpg" width="240" height="145" alt="Slough Town v Stourport Swifts" />
        </a>
    */
    ?>
    @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.

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2007
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Magic - thanks. Can I just ask how I would assign the NEW code that's produced to a variable created ie:

    PHP Code:
    $new_string='<a href="http://www.flickr.com/photos/horshamrebel/4040847816/" title="Slough Town v Stourport Swifts"><img src="http://farm4.static.flickr.com/3531/4040847816_0d926a6f32_m.jpg" width="240" height="145" alt="Slough Town v Stourport Swifts" /></a>'
    As I don't know an awful lot about simple xml


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
  •