Reputation: 855
I have a text with a lot of words and links. I want to find a specific link inside it. I currently use:
preg_match('/http:\/\/www\.myserver\.com\/.*\.(rar|zip|tar|gz)/',$text,$result)
Now let's say i have this text:
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla <a href="http://www.myserver.com/test.zip">http://www.myserver.com/somefile.zip</a> bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
the expected result would be:
But the script will return:
http://www.myserver.com/test.zip">http://www.myserver.com/somefile.zip
I suppose i need to tell it to stop after reaching the first ".zip" but... how ?
Upvotes: 3
Views: 1777
Reputation: 137398
The problems is that preg_match
by default is greedy. This means it will match as much of the string as possible.
If you add a ?
to a quantifier, it will make it non-greedy, meaning "stop where the next pattern begins"
preg_match('/http:\/\/www\.myserver\.com\/.*?\.(rar|zip|tar|gz)/',$text,$result);
// ----^
See also:
Upvotes: 2