使用正则表达式识别 WordPress 短代码

发布于 2024-12-17 01:49:39 字数 340 浏览 3 评论 0原文

我有一个 WordPress 短代码,其中包含一些其他短代码。当执行第一个短代码时,我想使用正则表达式过滤掉其他短代码。

[main_code]
   [sub_code id='testid']test content[/sub_code]
   [sub_code id='testid' name='testname']test content[/sub_code]
[/main_code]

当我执行 main_code 时,我想将 sub_code 过滤到数组中并访问其属性,而不将 sub_code 作为短代码执行。

任何有知识可以为我提供解决方案的人都会受到高度赞赏。

I have a wordpress shortcode which contains some other shortcodes inside it. When the first shortcode is executed i want to filter out other shortcodes using a regex.

[main_code]
   [sub_code id='testid']test content[/sub_code]
   [sub_code id='testid' name='testname']test content[/sub_code]
[/main_code]

When i execute the main_code i want to filter the sub_code into an array and access its attributes without executing sub_code as a shortcode.

Anyone who has knowledge to give me a solution is highly appriciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法唧唧 2024-12-24 01:49:39

如果您想匹配内部部分,那么我建议:

preg_match_all('~\[sub_code([^\[\]]*)]([^\[\]]+)\[/sub_code]~', $content, $result);

[^\[\]] 匹配任何不带方括号的内容。因此可以确保其中不能存在其他短代码。

If you want to match the inner parts, then I'd advise:

preg_match_all('~\[sub_code([^\[\]]*)]([^\[\]]+)\[/sub_code]~', $content, $result);

The [^\[\]] matches any content without square brackets. So it's ensured no other shortcodes can exist within.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文