Reputation: 71
I want to get matches on a webpage based on following regular expression: (.*) I tested it on regexpal.com (an online regular expression test tool) and it works fine. However, when I use it in php, I can't find any matches. The statement I use in php is
preg_match_all("/<a href=\"\/title\/.*\/\">(.*)<\/a>/", $content, $matches);
I checked the $content, it's correct. So is there anything wrong from my statement? Thanks!
Upvotes: 1
Views: 1533
Reputation: 12985
Please, please... for the love of God, don't wrap Regular Expressions that deal with URLs or HTML in /. You have to escape it all over the place. It's terrible. Look here:
preg_match_all('~<a href="/title/[^">]+/">(.*?)</a>~si', $content, $matches);
There's more ways to improve this but this should do it.
Hope it helps.
Upvotes: 7
Reputation: 1090
preg_match_all("/<a href\=\"\/title\/.*\/\">(.*?)<\/a>/", $content, $matches);
I would try:
preg_match_all('/<a href\=".title.*">(.*?)<\/a>/', $content, $matches);
for brevity.
Upvotes: 0
Reputation: 43673
You need to make your regex pattern lazy (non-greedy) by adding ?
>>
preg_match_all("/<a href=\"\/title\/.*?\/\">(.*?)<\/a>/", $content, $matches);
Upvotes: 1