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:

$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, $https, PREG_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

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

Try this

$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];
}

It’s the pattern delimiter (I prefer # instead of /)

If your delimiter is #, why are you escaping the /'s ?

gvre: Thank you for the response!
StarLion: If one uses # instead of / then we dont’ have to do the escaping?

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.reference.delimiters.php

Perfect! Thanks so much…

by mistake :slight_smile: