Using PHP
, I have the folloowing code:
$text = '[text 1] highlight("ABC DEF") [text 2] highlight("GHI JKL") [text 3] [text 4]';
Then I want to catch
the following groups
:
group 1: highlight("ABC DEF") [text 2]
group 2: highlight("GHI JKL") [text 3] [text 4]
I tried the following:
preg_match_all('/highlight.*/', $text, $matches);
print_r($matches);
but I get:
Array
(
[0] => Array
(
[0] => highlight("ABC DEF") [text 2] highlight("GHI JKL") [text 3] [text 4]
)
)
But that's not what I want because it is all together.
I also tried:
preg_match_all('/highlight/', $text, $matches);
print_r($matches);
but I get:
Array
(
[0] => Array
(
[0] => highlight
[1] => highlight
)
)
And that's not what I want neither.
Any idea what regexp
to use in order to get the groups
I mentioned above
?
<?php
$matches = array();
preg_match_all("/highlight\([^)]*\) .*?(?= highlight|$)/", '[text 1] highlight("ABC DEF") [text 2] highlight("GHI JKL") [text 3] [text 4]', $matches);
var_dump($matches);
For explanations: