Reputation: 421
I'm tearing my hair out with this one - it can't be this complicated, but have looked around on the internet and can't find the answer..
I have a string which could be either of the following:
I want to extract the amount(s) from the string, but only when "day" or "pd" occurs in the string.
What i have so far is...
preg_match_all('/([0-9.]+).*(day|pd)/i', $string, $matches)
.. but it doesn't seem to pull out the 2nd amount. I could narrow it down to..
preg_match_all('/([0-9.]+)/', $string, $matches)
.. but then it would match those which didn't contain "day" or "pd".
Any help would be appreciated!
Upvotes: 1
Views: 62
Reputation: 39365
Try this one:
preg_match_all('/(?=£([0-9.]+).*(?:day|pd))/i', $string, $matches);
Upvotes: 0
Reputation: 785216
You can use lookahead instead:
if ( preg_match_all('/[0-9.]+(?=.*?(?:day|pd))/i', '£300.00-£325.00 per day', $matches) )
print_r($matches[0]);
Array
(
[0] => 300.00
[1] => 325.00
)
Upvotes: 1