php - reg exp match everything inside curly braces -


i have php code , want match inside curly braces {}

$sentence= "this {is|or|and} {cat|dog|horse} {kid|men|women}"; preg_match("/\{.*?\}/", $sentence, $result); print_r($result); 

but output:

array ( [0] => {is|or|and} )  

but need result this:

array ( [0] => is|or|and [1] => cat|dog|horse [2] => kid|men|women  )  

what regular expression should use?

use preg_match_all instead?

preg_match_all("/\{.*?\}/", $sentence, $result); 

if don't want braces, can 2 things:

capture parts inside braces , use $result[1] them hamza correctly suggested:

preg_match_all("/\{(.*?)\}/", $sentence, $result); print_r($result[1]); 

or use lookarounds (they might bit difficult understand however):

preg_match_all("/(?<=\{).*?(?=\})/", $sentence, $result); print_r($result[0]); 

note can use [^}]* instead of .*?, considered safer.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -