PHP - matching with all existing phrases in text - preg_replace

Here is my text:

$msg_text = '[quote]TEXT[/quote]';

and my preg_replace:

$msg_text = preg_replace('#\[quote\](.*?)\[\/quote\]#is', '"$1"'."\r\n", $msg_text);

And it works great. But what when my text looks like this:

$msg_text = '[quote]TEXT [quote]TEXT[/quote][/quote]';

?? In this example, my preg_replace is not working. How can I replace this text in all cases?

+4
source share
1 answer

Just delete ?after .*, which will remove the laziness of your template.

'#\[quote\](.*)\[\/quote\]#is'

What is laziness in Regex?

, .* .+ , , , . ? , , , / . , , .

. ? , , , , *, .

: http://www.regular-expressions.info/repeat.html

+1

Source: https://habr.com/ru/post/1620120/


All Articles