I am trying to do a preg_replace so that i can format some text outputted from another program that doesnt look exactly how i want it to ....
here is a the snippet....
zzz:xxx1 _:somewhereElse_1 ;
xxx:yyy "dddd" ;
zzz:xxx2 _:somewhereElse_2 ;
yyy:zzz "ffff" ;
_:somewhereElse_1
I am somewhere else ;
_:somewhereElse_2
whatsup ;
What i am trying to do is make it look like this...
zzz:xxx1 I am somewhere else ;
xxx:yyy "dddd" ;
zzz:xxx2 whatsup ;
yyy:zzz "ffff" ;
I have been trying to do it in various stages...
Stage 1. Extract the _:identifier from the top bit...
preg_match_all("/.*:.*_:somewhereElse_[(0-9)]+/", $data, $matches);
That works But what I cant do is extract the corresponding value from the bottom bit... none of my dam regular expressions work.
If you bear in mind that i have extracted all the somewhereElse_n the closest i can get is
preg_match_all("/(
$somewhereElse_n
.*;)?/s", $data, $matches);
The problem is the matches array as loads of empty places (nearly 2000) and its matching all the somewhereElse's instead of stopping after the first ..
[1149] =>
[1150] =>
[1151] =>
[1152] =>
_:somewhereElse_1
I am somewhere else ;
So am i;
_:somewhereElse_2
whatsup ;
[1153] =>
[1154] =>
Any help would be gratefully accepted :D
Your .*
(any amount of anything) is "greedy", and tries to match as much as it possibly can while still being valid. As such, it's matching from the first match all the way to the last one.
Changing it to .*?
will make it non-greedy, which makes it match as little as possible while still remaining valid. As such, it should then match each individual occurrence as a separate match, instead of bundling them all into one.