在PHP中的括号之间匹配文本 [英] 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?
具体地说,我正在编写Wordpress插件,并希望重新格式化两个花括号之间的所有文本(准维基标记).
Specifically, I am writing a Wordpress plugin and am looking to reformat all text between two curly braces (a quasi wiki-marking).
我已经按照另一个先前的问题中所述的步骤进行操作我问,并且 matching 部分起作用了-这是我需要帮助的 match .
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.
示例:
This is some {{text}} and I want to reformat the items inside the curly braces
所需的输出:
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);
这篇关于在PHP中的括号之间匹配文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!