0

I have an example:

some text lorem ipsum ... {gallery}../2021/some_string_2021/some_other_string{/gallery} ... other lorem ipsum

And I need to get out only the ../2021/some_string_2021/some_other_string.

How can I ignore all before {gallery} and all after {/gallery} while being able to capture ../2021/some_string_2021/some_other_string?

Tried to figure it out but cannot make it work neither with some sort of combination with:

preg_match_all("/\[(.*?)\]/", $text, $matches);
print_r($matches[1]);

if( preg_match( '!\(([^\)]+)\)!', $text2, $match ) )
echo $match[1];

Maybe I should first change [] to {}, and then try it with (.*?){(.*?)} ?

2
  • 1
    preg_match('#(?<=\{gallery}).*?(?=\{/gallery})#', $str, $matches); echo $matches[0]; Commented Feb 2, 2021 at 7:12
  • For what it's worth... The lookbehind in Nick's comment is more expensive than necessary. #\{gallery}\K.*?(?=\{/gallery})# on the sample string will reduce the step count from 191 to 110. Furthermore, capturing and referencing the opening tagname with #\{(gallery)}\K.*?(?=\{/\1})# will reduce the step count down to 107 while reducing the pattern length and eliminating the possible occurrence of typo-mismatching between the opening and closing tagname. Commented Feb 2, 2021 at 22:35

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.