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: <img src="http://latex.codecogs.com/gif.latex?x^2&plus;3x-4" alt="x^2+3x-4" /> is one such formula and ‚another formula is <img src="http://latex.codecogs.com/gif.latex?\\frac{a}{b}" alt="\\frac{a}{b}" /> this one. </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?
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”
$string = '<p>Id like to add this stuff here: <img src="http://latex.codecogs.com/gif.latex?x^2&plus;3x-4" alt="x^2+3x-4" /> is one such formula and ‚another formula is <img src="http://latex.codecogs.com/gif.latex?\\frac{a}{b}" alt="\\frac{a}{b}" /> this one. </p>';
$pattern = '#src="(http:\\/\\/latex.codecogs.com[^"]+)"#s';
if (preg_match_all($pattern, $string, $m))
{
$matches = $m[1];
}
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 \#.