My text is something like this:
[if-abc]content of abc[/if] [if-def]content of def[/if]
i want to match:
[if-abc]content of abc[/if] and then [if-def]content of def[/if]
but my current function matches the entire thing. how can i make it so it matches the first occurence of the closing [/if] tag
$m = null;
preg_match_all('/\[if-([a-z-]*)\](.*)\[\/if\]/', $source, $m);
This works except when multiple conditions are present on the same line.
Make your star quantifier non-greedy with...
preg_match_all('/\[if-([a-z-]*)\](.*?)\[\/if\]/', $source, $m);
This way the .*
construct will start matching from 'no symbols at all' state, gradually widening it, one symbol at a time - and will stop doing it when it encounters the first [/if]
part in the string's flow. Otherwise it actually starts with consuming all the string, releasing one symbol at a time - and stops when it faces the last [/if]
part in the string's flow.
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.