SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: URL regex

  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    URL regex

    Hello, I'm trying to pull off the url's within the sorce of my $string below using preg_match_all. I'm currently using:

    PHP Code:
    $string '<p>Id like to add this stuff here:&nbsp;<img src="http://latex.codecogs.com/gif.latex?x^2&amp;plus;3x-4" alt="x^2+3x-4" />&nbsp;is one such formula and&nbsp;&sbquo;another formula is&nbsp;<img src="http://latex.codecogs.com/gif.latex?\frac{a}{b}" alt="\frac{a}{b}" />&nbsp;this one.&nbsp;</p>';

     
    preg_match_all('/"http:\/\/(latex.codecogs.com)\/(gif|svg|png)\.latex\?((.^\")*)"/'$string$httpsPREG_SET_ORDER); 
    Basically, each url will start with http://latex.codecogs.com/gif.latex?, but then all bets are off. I think that my regex is correct up through ((.^\")*) which is my attempt at saying "Hey, look for any pattern here except for a quote and then find the final quote. Unfortunately, nothing is found. Any thoughts?

    Thank you,

    Eric

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this

    PHP Code:
    $string '<p>Id like to add this stuff here:&nbsp;<img src="http://latex.codecogs.com/gif.latex?x^2&amp;plus;3x-4" alt="x^2+3x-4" />&nbsp;is one such formula and&nbsp;&sbquo;another formula is&nbsp;<img src="http://latex.codecogs.com/gif.latex?\frac{a}{b}" alt="\frac{a}{b}" />&nbsp;this one.&nbsp;</p>';

    $pattern '#src="(http:\/\/latex.codecogs.com[^"]+)"#s';
    if (
    preg_match_all($pattern$string$m))
    {
            
    $matches $m[1];


  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much for the response! The pattern works perfectly and almost makes perfect sense. If you wouldn't mind explaining the # at the beginning and the #s at the end, I'd greatly appreciate it...you know, the whole "Give a man a fish vs. Teach a man to fish"

    -Eric

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's the pattern delimiter (I prefer # instead of /)

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    If your delimiter is #, why are you escaping the /'s ?

  6. #6
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    gvre: Thank you for the response!
    StarLion: If one uses # instead of / then we dont' have to do the escaping?

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    most of the examples you see in the manual use / as a delimiter - which is why they have to escape it as \/. Using a different delimiter removes this requirement. (However, if, for example, you wanted to use a literal # in the pattern above, you WOULD have to escape it as \#.

    Take a look at the second section of this page of the manual: http://us3.php.net/manual/en/regexp....delimiters.php

  8. #8
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Perfect! Thanks so much....

  9. #9
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    If your delimiter is #, why are you escaping the /'s ?
    by mistake


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
  •