开发者

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 i开发者_StackOverflow中文版s 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.


$content = preg_replace('/{([^{}]*)}/', "<strong>$1</strong>", $content);


You need to form a match group using ( round braces ).

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

Whatever (.+?) matches then can be used as $1 in the replacement string. This way you have the enclosing {{ and }} already out of the way. Also \b was redundant.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜