Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Help on extract url and image

Last post 06-25-2008, 7:59 PM by Lyndar. 1 replies.
Sort Posts: Previous Next
  •  06-25-2008, 9:29 AM 43468

    Help on extract url and image

    Hello how's everyone doin'? I need help with the regular expression in PHP (using preg_match_all) that would extract the url and the image of these two:

    <A HREF="movie1.mpg"><img name="movie1" src="images/tn_01.jpg" width="320" height="240" border="0" alt="Play Movie" title="Play Movie" class="vid_th"></A>

    <a href="movie2.mpg"><img src="images/tn_01.jpg" width=320 height=240 border=0 class="fhg_vid"><br><img src="images/play.gif" width=320 height=48 border=0></a>

    If you notice on the second one, there are two image sources: "images/tn_01.jpg" and "images/play.gif". I would only need to extract the first image source ("images/tn_01.jpg").
    Also, I would only need to extract jpg image formats.

    Currently, my expression is:
    $expr = '@<a[^>]*href="([^"]*)"[^>]*>[^>]*<img[^>]*src="([^"]*)"[^>]*>[^>]*</a>@Usi';

    This works fine with the first given but it doesnt extract the jpgs with the second one. I'm new with regexp so i'm still a little confused. I would appreciate it very much if you could provide an explanation with your expression.

    Thanks much! have a nice day y'all!!!

  •  06-25-2008, 7:59 PM 43491 in reply to 43468

    Re: Help on extract url and image

    Your regex is almost right.

    Change your last [^>]* to .*?

    Your regex assumes that there will be no > characters following the end of the first image tag.  Your second link contains two > characters prior to the closing </a> tag, so the match fails.

     

View as RSS news feed in XML