Reputation: 2984
Here is the code I'm using;
$string = "[if-protectDelete-{0}-][data]name[/data] can be deleted[/elseif][elseif-{1}-][data]name[/data] can't be deleted[/elseif][elseif-{2}-]No data[/elseif][/endif]";
if (preg_match_all("#\[elseif-\{(.+)\}-\](.+?)\[/elseif\]#", $string, $matches)) {
dumper($matches[0]);
}
$matches[0] output is;
array(1) {
[0]=> string(75) "[elseif-{1}-]PHP REGEX can't be deleted[/elseif][elseif-{2}-]No data[/elseif]"
}
I can get the part right for if, but elseif... It is totally different scenario I guess. Shouldn't it output like this?
array {
[0] => "[elseif-{1}-]PHP REGEX can't be deleted[/elseif]",
[1] => "[elseif-{2}-]No data[/elseif]"
}
Upvotes: 1
Views: 85
Reputation: 2255
It looks like you still have a greedy +
in there. This might fix it:
#\[elseif-\{([0-9]+)\}-\](.+?)\[/elseif\]#
Notice the [0-9]+
that replaces the .+
. Let me know how that goes.
Assuming you might have anything inside the curly braces, except other curly breaces, this would be even better:
#\[elseif-\{([^}]+)\}-\](.+?)\[/elseif\]#
Or, like you mentioned: .+?
Upvotes: 1
Reputation: 91385
I don't know what dumper
do but the array $matches
contains the whole matching in $matches[0]
and the groups captured in $matches[1]
$matches[2]
...
so you should do : print_r($matches);
to get your matches groups.
Upvotes: 1
Reputation: 694
Just add ?
your : \[elseif-\{(.+)\}-\](.+?)\[/elseif\]
right: \[elseif-\{(.+?)\}-\](.+?)\[/elseif\]
Upvotes: 1