I need find regex of follow string, and get all tab parent :
{jmstabs type="horizontal" theme="two" nav="left"}
[tab title="Tab title 1"]Tab content 1[/tab]
[tab title="Tab title 2"]Tab content 2[/tab]
[tab title="Tab title 3"]
{jmstabs}
[tab title="Tab child 1"]Tab child 1[/tab]
[tab title="Tab child 2"]Tab child 2[/tab]
{/jmstabs}
[/tab]
{/jmstabs}
I want to get result after preg_match_all is:
1. [tab title="Tab title 1"]Tab content 1[/tab]
2. [tab title="Tab title 2"]Tab content 2[/tab]
3. [tab title="Tab title 3"]
{jmstabs}
[tab title="Tab child 1"]Tab child 1[/tab]
[tab title="Tab child 2"]Tab child 2[/tab]
{/jmstabs}
[/tab]
Please help me! Thanks everybody.
You can use this pattern:
(?(DEFINE)
(?<item>\s*(?:(?&curly)|(?&square))|[^\[\{]*)
(?<attr>\s+\w+="[^"]*")
(?<curly>\{(\w+)(?&attr)*\}(?&item)*\{\/\w+\})
(?<square>\[(\w+)(?&attr)*\](?&item)*\[\/\w+\])
)\[tab(?&attr)*\](?&item)*\[\/tab\]
Thanks to @Rawing's Regex Pattern; below is how you could put those to use. And... by the way; You may check out a Demonstration here:
<?php
$str = '{jmstabs type="horizontal" theme="two" nav="left"}
[tab title="Tab title 1"]Tab content 1[/tab]
[tab title="Tab title 2"]Tab content 2[/tab]
[tab title="Tab title 3"]
{jmstabs}
[tab title="Tab child 1"]Tab child 1[/tab]
[tab title="Tab child 2"]Tab child 2[/tab]
{/jmstabs}
[/tab]
{/jmstabs}';
preg_match_all("#(?(DEFINE)
(?<item>\s*(?:(?&curly)|(?&square))|[^\[\{]*)
(?<attr>\s+\w+=\"[^\"]*\")
(?<curly>\{(\w+)(?&attr)*\}(?&item)*\{\/\w+\})
(?<square>\[(\w+)(?&attr)*\](?&item)*\[\/\w+\])
)\[tab(?&attr)*\](?&item)*\[\/tab\]#", $str, $matches);
var_dump( $matches[0] );
// PRODUCES:::
array (size=3)
0 => string '[tab title="Tab title 1"]Tab content 1[/tab]' (length=44)
1 => string '[tab title="Tab title 2"]Tab content 2[/tab]' (length=44)
2 => string '[tab title="Tab title 3"]
{jmstabs}
[tab title="Tab child 1"]Tab child 1[/tab]
[tab title="Tab child 2"]Tab child 2[/tab]
{/jmstabs}
[/tab]' (length=211)