Link to home
Start Free TrialLog in
Avatar of pixalax
pixalaxFlag for Poland

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.

$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.
ASKER CERTIFIED SOLUTION
Avatar of Ludwig Diehl
Ludwig Diehl
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pixalax

ASKER

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.
Hehe, I'm glad it was helpful. Regards!