Identifying wordpress shortcodes using a regular e

2019-06-13 03:52发布

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.

1条回答
放荡不羁爱自由
2楼-- · 2019-06-13 04:46

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.

查看更多
登录 后发表回答