直接跟进上一个question https://stackoverflow.com/questions/413071/regex-to-get-string-between-curly-braces-i-want-whats-between-the-curly-braces,如何使用 PHP 将文本(以及大括号,如果可能)作为匹配项拉出?
具体来说,我正在编写一个 Wordpress 插件,并希望重新格式化两个大括号之间的所有文本(准 wiki 标记)。
我已按照之前另一篇文章中概述的步骤进行操作question https://stackoverflow.com/questions/3443207/preg-replace-not-replacing-in-a-wordpress-plugin我问了,有matching部分工作 - 这是match我需要帮助。
Example:
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
我所拥有的(即not在职的):
$content = preg_replace('#\b\{\{`.+`\}\}\b#', "<strong>$0</strong>", $content);
如果匹配包括大括号太难了,我可以使用大括号作为偏移量进行匹配,然后使用更简单的文本匹配函数删除“有问题的”大括号。
$content = preg_replace('/{([^{}]*)}/', "<strong>$1</strong>", $content);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)