Short answer: Yes you can. You need to use lazy quantifiers. Therefore, instead of
preg_match("/[[test.*]]/i",$str,$match);
use
preg_match("/\[\[test.*?\]\]/i",$str,$match);
so that the function stops in the first match. Note: if you want to match an alphabetic character [ or ] , you need to escape from them, for example: \[ or \] .
After a little reaserch on php.net, I found a template modifier U (PCRE_UNGREEDY) that sets the default value for the template as lazy as greedy.
So this means that
preg_match("/\[\[test.*\]\]/iU",$str,$match);
also suitable for this purpose. The modifier U will do everything * , + ? in the regular expression as few characters as possible. In addition, quantifiers that were jagged ( *? +? And ?? ) will now become greedy (match as many characters as possible).
source share