Matching text between curly braces in PHP

In direct observation of the previous question , how can I pull out the text (and, if possible, curly braces), as a match using PHP?

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

I followed the steps described in the previous question I asked and processing the relevant part is a coincidence in which I need help.

Example:

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

Required Conclusion:

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

Whats mine (what doesn't work):

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

If the match , including the brackets, is too complicated, I can match using curly braces as offsets, and then remove the β€œoffensive” brackets using the simpler text matching function.

+4
source share
2 answers
 $content = preg_replace('/{([^{}]*)}/', "<strong>$1</strong>", $content); 
+6
source

You need to form a matching group using ( parentheses ) .

 preg_replace('#\{\{(.+?)\}\}#', "<strong>$1</strong>", 

Whatever matches (.+?) , It can be used as $1 in the replacement string. So you already have {{and}}. Also \b was redundant.

+4
source

Source: https://habr.com/ru/post/1337963/


All Articles