OhhMee OhhMee - 1 year ago 61
PHP Question

Regex capture text between two dynamic texts

First of all I would like to admit that I am not good at regex.

I have this string:

[my_section title="Foo" the_id="123"][my_text]my example text[/my_text][/my_section]

What is possible way to capture everything except
[my_section title="Foo" the_id="123"]
or simply saying any text between these two?

My knowledge on this is not working at all.

I cannot use literal
[my_section title="Foo" the_id="123"]
because the params after
is dynamic.

Answer Source

How about:

preg_match('~\[my_section[^\]]*\](.+?)\[/my_section\]~', $string, $match);

The string you want is in $match[1]