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.

$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);
		}

Open in new window


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"
}

Open in new window


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]";

Open in new window


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]";

Open in new window


Then output displays as it should
array(3) {
  [0]=>
  string(2) "id"
  [1]=>
  string(2) "id"
  [2]=>
  string(3) "cat"
  [3]=>
  string(4) "name"
}

Open in new window


 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.
LVL 2
pixalaxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ludwig DiehlSystems ArchitectCommented:
this should work. You will notice only found[1] has the values u need

if (preg_match_all("#\[data\](.+?)\[/data\]#", $string, $founds)) {

}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pixalaxAuthor Commented:
Thank you for your reply. I swear I tried this and many other ways didn't work but now it does work.

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.
0
Ludwig DiehlSystems ArchitectCommented:
Hehe, I'm glad it was helpful. Regards!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.