Identifying wordpress shortcodes using a regular expression

流过昼夜 提交于 2019-12-08 11:00:00

问题


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:


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.



来源:https://stackoverflow.com/questions/8207098/identifying-wordpress-shortcodes-using-a-regular-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!