Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following short codes:

Dear {{name}},

You are being invited for the following event: {{event}}

regards, {{author}}

I have an array from the database : $data

where:

$data['name'] = 'John Doe';
$data['event'] = 'Party yay!';
$data['author'] = 'Kehke Lunga';

Output that I expect:

Dear John Doe,

You are being invited for the following event: Party yay!

regards, Kehke Lunga

also, I also want to perform operations like {{firstname||lastname}} which should either check if key $data['firstname'] is set, if it isn't it should use $data['lastname']. However, that is for later stage.

For now, I just want to know how to match the text between 2 curly braces.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
808 views
Welcome To Ask or Share your Answers For Others

1 Answer

With preg_match_all():

$pattern = '~{{(.*?)}}~';
preg_match_all($pattern, $string, $matches);
var_dump($matches[1]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...