Page 1 of 1
regex prob
Posted: Tue Apr 20, 2004 12:42 pm
by eNtropicChild
I'm trying to fix a csv file that got messed up when one of the columns has a bunch of line returns in it. Basically all i need to know how to do is be able to match all the data that lies between the words "st1nk1" and "st1nk2" and it must include spaces and new lines. I'm havin a hell of a time tryin to figure out the pattern i need to use. Please Help! Thanks.
Posted: Tue Apr 20, 2004 4:51 pm
by tim
not tested, probably wrong: but
Code: Select all
$word[] = '/st1nk1(.+?)st1nk2/';
$replace[] = '<b>\\1</b>'; // this grab the text between the two words n turn it bold, change the bold tags to whatever you wish to do with the info.
foreach($word as $key => $value) {
$string = preg_replace($value, $replace[$key], $string);
}
return $string;
Posted: Tue Apr 20, 2004 5:02 pm
by feyd
tim wrote:not tested, probably wrong: but
Code: Select all
$word[] = '/st1nk1(.+?)st1nk2/';
$replace[] = '<b>\\1</b>'; // this grab the text between the two words n turn it bold, change the bold tags to whatever you wish to do with the info.
foreach($word as $key => $value) {
$string = preg_replace($value, $replace[$key], $string);
}
return $string;
preg_replace accepts arrays as arguments for the first two parameters. So one could simplify this instance by:
Code: Select all
$word[] = '/st1nk1(.+?)st1nk2/';
$replace[] = '<b>\\1</b>'; // this grab the text between the two words n turn it bold, change the bold tags to whatever you wish to do with the info.
$string = preg_replace($word, $replace,$string);
Posted: Tue Apr 20, 2004 5:07 pm
by tim
fully aware of that
I was considering he would be adding more paramaters?
if not, the above code is all u need.
Posted: Tue Apr 20, 2004 5:22 pm
by feyd
adding more to each of those arrays will still work and execute each one.
Posted: Tue Apr 20, 2004 6:04 pm
by eNtropicChild
Thanks, that helped alot. This is the code i ended up using:
Code: Select all
unset($match);
preg_match("/st1nk1(.+?)st1nk2/s",$boomї$i],$match);
$match = ereg_replace("\n"," -- ",$matchї0]);
$match = ereg_replace("st1nk1","",$match);
$match = ereg_replace("st1nk2","",$match);
$boomї$i] = preg_replace("/st1nk1(.+?)st1nk2/s",$match,$boomї$i]);