Rakhitha Nimesh
Rakhitha Nimesh

Reputation: 1451

Identifying wordpress shortcodes using a regular expression

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.

Upvotes: 0

Views: 2240

Answers (1)

mario
mario

Reputation: 145482

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.

Upvotes: 1

Related Questions