SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Regex Problem

  1. #1
    SitePoint Zealot j3ph's Avatar
    Join Date
    Jan 2006
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regex Problem

    I'm trying to read the address of an image located on another web page. The image is wrapped in an anchor tag that has an id attached to it. The html for the link and the image appears like this:

    Code:
    <a id="uniqueid" href="#">
    <img src="path-to-image.jpg" />
    </a>
    I can read as far as the anchor tag, but can't seem to figure out how to find the path of the image on the next line down.

    I could just read the next line in the file, but I'm afraid if they ever altered the white space on the other page, my code would stop working properly. Any thoughts?

  2. #2
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this?

    PHP Code:
    $string '<a id="uniqueid" href="#">
    <img src="path-to-image.jpg" />
    </a>'
    ;

    $matches = array();
    preg_match('/id="uniqueid"[^<>]*>\s*<img[^<>]*src="([\w-\.:\/]*)"/i'$string$matches);

    echo 
    $matches[1] . "\n"
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.


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
  •