Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all occurrences of words between curly brackets

Tags:

regex

php

I have a text like:

This is a {demo} phrase made for {test}

I need to get

demo  
test

Note: My text can have more than one block of {}, not always two. Example:

This is a {demo} phrase made for {test} written in {English}

I used this expression /{([^}]*)}/ with preg_match but it returns only the first word, not all words inside the text.

like image 677
Frank Avatar asked Nov 09 '11 14:11

Frank


1 Answers

Use preg_match_all instead:

preg_match_all($pattern, $input, $matches);

It's much the same as preg_match, with the following stipulations:

Searches subject for all matches to the regular expression given in pattern and puts them in matches in the order specified by flags.

After the first match is found, the subsequent searches are continued on from end of the last match.

like image 102
Grant Thomas Avatar answered Nov 06 '22 11:11

Grant Thomas