regex prob
Moderator: General Moderators
-
eNtropicChild
- Forum Newbie
- Posts: 2
- Joined: Tue Apr 20, 2004 12:42 pm
- Location: Santa Monica
- Contact:
regex prob
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.
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;- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
preg_replace accepts arrays as arguments for the first two parameters. So one could simplify this instance by: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;
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);-
eNtropicChild
- Forum Newbie
- Posts: 2
- Joined: Tue Apr 20, 2004 12:42 pm
- Location: Santa Monica
- Contact:
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]);