Reputation: 1631
I am trying to match a string from a large string.
example
"quick brown fox is here but quick brown fox always over there too"
preg_match('(quick brown fox(.+?)too)',$txt,$match)
the problem is it grabs the whole sentence rather than "quick brown fox always over there too"
How do I grab only the second part?..this happens when there are similar strings are together in one line or so...
Upvotes: 1
Views: 159
Reputation: 36244
Non-greedyness is applied only at the end of the pattern (actually, it means, that if prce finds a match, doesn't look for a larger match, just returns), so:
"quick brown fox is here too, and always over there too"
preg_match('(quick brown fox(.+?)too)', $txt, $match)
would match only "quick brown fox is here too" - but nothing, what you're looking for.
Maybe you could reverse your $txt with strrev()
& write a reversed pattern, than reverse your matches too, but it will only solve this specific problem.
Upvotes: 1
Reputation: 1122
You have 2 capturing groups here. Your $match
array will contain..
[0] => quick brown fox is here but quick brown fox always over there too
[1] => quick brown fox is here but quick brown fox always over there too
[2] => is here but quick brown fox always over there
Also, you're missing the two forward slashes in your regex.
Upvotes: 1