Ignore characters in preg_match_all output

I have this regex:

preg_match_all('/{.*?}/', $html, $matches);

Returns all lines written inside curly braces. The $ matches variable also contains the characters {and}. How to remove them?

I do not want to do:

if ($matches[0] == "{variable}")

And I do not want to add (and) characters to the regular expression because I do not want to use:

preg_match_all('/{(.*?)}/', $html, $matches);
if ($matches[0][0] == "variable")

So, is there an easier way to remove curly braces from $ matches in a regular expression?

+4
source share
3 answers

PCRE ( PHP ) lookarounds . Lookbehind, (?<=...), , . (?=...) , . : (?<!...) (?!...).


:

(?<={).*?(?=})


:

preg_match_all('/(?<={).*?(?=})/', $html, $matches);
// $matches[0] = 'variable';

@CasimirEtHippolyte . , - - . .*? [^}]*, 0+ }.

+7

reset { , }. {} , }

{\K[^}]*

. regex101

+3
(?<={).*?(?=})

Replace your regular expression with this. This will work.

+2
source

All Articles