4

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 fine. But what when my text is looking like that:

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

?? My preg_replace doesn't work on this example. How I can replcace this text in all instances ?

Majkelo
  • 53
  • 7

1 Answers1

1

Just remove the ? after .* which will remove the laziness of your pattern.

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

What is Laziness in Regex?

Patterns like .* and .+ normally greedy, which means they will try to match as long as they can. Putting an extra ? after it we tell it to become lazy, so it will take only nearest / shortest match. So, if you remove that, it will work as it's normal way.

Note: Other use of ? is to make the previous item optional, which is not the case here, because by using * you are making that part optional already.

Learn more from here : http://www.regular-expressions.info/repeat.html