pixalax
asked on
PHP regex problem, can't get it right.
Hello all,
I'm newbie with regex so I guess it is normal to have problems but this one seriously made me angry enough, I hope you experts can come to my aid.
When I dump the data I have following output
As you can see it is grabbing everything I need just the way it should except one small glitch.
If I change my $string from
to (just plain & simple `enter` button)
Then output displays as it should
I don't understand why this regex doesn't work if I have more than 1 [data]xxx[/data] in the same line. If I have a new line, than it works perfectly. I am thinking of my regex, maybe it is not correct.
I would be glad if any expert could help me out with my problem. Thanks in advance.
I'm newbie with regex so I guess it is normal to have problems but this one seriously made me angry enough, I hope you experts can come to my aid.
$string = "
This is link for Viewing Profile : [data]id[/data] <br />
[level-1-] This is level 1 test of data [data]id[/data][/level] <br />
[level-0-][if-protectDelete-{0}-]Delete ID NO: [data]cat[/data], NAME :[data]name[/data] with protection[/endif][/level]";
if (preg_match_all("#(.*?)\[data\](.+)\[/data\](.*?)#", $string, $founds)) {
dumper($founds[2], TRUE);
}
When I dump the data I have following output
array(3) {
[0]=>
string(2) "id"
[1]=>
string(2) "id"
[2]=>
string(28) "cat[/data], NAME :[data]name"
}
As you can see it is grabbing everything I need just the way it should except one small glitch.
If I change my $string from
$string = "
This is link for Viewing Profile : [data]id[/data] <br />
[level-1-] This is level 1 test of data [data]id[/data][/level] <br />
[level-0-][if-protectDelete-{0}-]Delete ID NO: [data]cat[/data], NAME :[data]name[/data] with protection[/endif][/level]";
to (just plain & simple `enter` button)
$string = "
This is link for Viewing Profile : [data]id[/data] <br />
[level-1-] This is level 1 test of data [data]id[/data][/level] <br />
[level-0-][if-protectDelete-{0}-]Delete ID NO: [data]cat[/data],
NAME :[data]name[/data] with protection[/endif][/level]";
Then output displays as it should
array(3) {
[0]=>
string(2) "id"
[1]=>
string(2) "id"
[2]=>
string(3) "cat"
[3]=>
string(4) "name"
}
I don't understand why this regex doesn't work if I have more than 1 [data]xxx[/data] in the same line. If I have a new line, than it works perfectly. I am thinking of my regex, maybe it is not correct.
I would be glad if any expert could help me out with my problem. Thanks in advance.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hehe, I'm glad it was helpful. Regards!
ASKER
This means only 1 thing, I was too angry to even check if I saved the file or not (was trying one version after another to find a fix).
Thank you very much for your help once again.