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?
 
Ludwig DiehlConnect With a Mentor Systems ArchitectCommented:
this should work. You will notice only found[1] has the values u need

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

}
0
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.