Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching text between braces in PHP

In direct follow-up to this previous question, how can I pull the text (and the braces if possible) out as a match using PHP?

Specifically, I am writing a Wordpress plugin and am looking to reformat all text between two curly braces (a quasi wiki-marking).

I've followed the steps outlined in another previous question I asked, and have the matching part working - it's the match I need help with.

Example:

This is some {{text}} and I want to reformat the items inside the curly braces

Desired output:

This is some *Text fancified* and I want to reformat the items inside the curly braces

What I have (that is not working):

$content = preg_replace('#\b\{\{`.+`\}\}\b#', "<strong>$0</strong>", $content);

If matching including the braces is too difficult, I can match using the braces as offsets, and then remove the 'offending' braces afterwards, too, using a more simple text-match function.

like image 908
warren Avatar asked Dec 28 '22 02:12

warren


1 Answers

$content = preg_replace('/{([^{}]*)}/', "<strong>$1</strong>", $content);
like image 54
Rami Dabain Avatar answered Jan 05 '23 18:01

Rami Dabain