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

Match image URLs

Last post 07-09-2008, 5:50 AM by ddrudik. 3 replies.
Sort Posts: Previous Next
  •  07-08-2008, 4:49 PM 43912

    Match image URLs

    I'm using the following regular expression in PHP to match png and jpg URLs with unusual characters like parentheses, commas and some international characters in them:

     #http://[a-z\dçñ_(),.%&\-+/\s]+(png|jpe?g)#i';

    Is there a simpler regular expression to do the same?

     

    Thanks in advance

     

  •  07-08-2008, 6:02 PM 43914 in reply to 43912

    Re: Match image URLs

    Just a guess on the limited details given:

    (?is)(?<=(["']))http://(?:(?!\1).)*?(?:\.(?:jpe?g|gif|png))(?=\1)


  •  07-09-2008, 3:28 AM 43933 in reply to 43914

    Re: Match image URLs

    I can't get your regular expression to work with preg_match. It gives me following error:

    "reference to non-existent subpattern at offset 39"

    I just need a regular expression that matches strings like:

    http://domain:optionalport/filename path with any kind of character in it (not only alphanumeric characters).jpg or png extension

     

     

  •  07-09-2008, 5:50 AM 43937 in reply to 43933

    Re: Match image URLs

    Possibly a code issue, please try this code:

    <?php
    $sourcestring='test<img src="HTTP://test/test.jpg">test';
    preg_match_all('#(?is)(?<=(["\']))http://(?:(?!\1).)*?(?:\.(?:jpe?g|png))(?=\1)#',$sourcestring,$matches);
    echo "<pre>".print_r($matches,true);
    ?>


View as RSS news feed in XML