使用正则表达式识别 wordpress 短代码 [英] Identifying wordpress shortcodes using a regular expression

查看:24
本文介绍了使用正则表达式识别 wordpress 短代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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]

当我执行 main_code 时,我想将 sub_code 过滤到一个数组中并访问其属性,而不将 sub_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.

推荐答案

如果要内搭的话,我建议:

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.

这篇关于使用正则表达式识别 wordpress 短代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆