Reputation: 2649
I have a string with a random amount of words, and I need to find the random texts that is in between the words, one and two, and after the word two. I managed to get the random texts in between the words, one and two, but after the word two, I'm only getting one character of the random texts. Please take a look at my code, and let me know what I did wrong.
$string = 'one randomText two randomText';
preg_match_all('/one\\s+(.+?)\\s+two\\s+(.+?)/i', $string, $matches);
print_r($matches);
Expected output:
Array ( [0] => Array ( [0] => one randomText two randomText ) [1] => Array ( [0] => randomText ) [2] => Array ( [0] => randomText ) );
Actual output:
Array ( [0] => Array ( [0] => one randomText two r ) [1] => Array ( [0] => randomText ) [2] => Array ( [0] => r ) );
Upvotes: 0
Views: 39
Reputation: 785128
You can use:
preg_match_all('/one\s+(.+?)\s+two\s+(.+)/i', $string, $matches);
No need to use non-greedy (lazy) quantifier here in the end which will otherwise match as little as possible (more than one) hence matches only r
.
Upvotes: 4