Currently I have this regexp to detect strings between double curly brackets and it work's wonderfully.
$str = "{{test}} and {{test2}}";
preg_match_all('/(?<={{)[^}]*(?=}})/', $str, $matches);
print_r($matches);
Returns:
Array
(
[0] => Array
(
[0] => test
[1] => test2
)
)
Now I need to expand it to only match stuff between ]] and [[
$str = "{{dont match}}]]{{test}} and {{test2}}[[{{dont match}}";
I've been trying to modify the regex but the lookahead and lookbehind is making it too difficult for me. How can I get it to match stuff inside ]] and [[ only?
Also I would like to match the whole string between ]] and [[ and then I would like to match each individual string between {{ }} inside it.
For example:
$str = "{{dont match}}]]{{test}} and {{test2}}[[{{dont match}}";
Would return:
Array
(
[0] => Array
(
[0] => {{test}} and {{test2}}
[1] => test
[2] => test2
)
)
Piggyback using preg_replace_callback
:
$str = "{{dont match}}]]{{test}} and {{test2}}[[{{dont match}}";
$arr = array();
preg_replace_callback('/\]\](.*?)\[\[/', function($m) use (&$arr) {
preg_match_all('/(?<={{)[^}]*(?=}})/', $m[1], $arr); return true; }, $str);
print_r($arr[0]);
Output:
Array
(
[0] => test
[1] => test2
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.