regex prob

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eNtropicChild
Forum Newbie
Posts: 2
Joined: Tue Apr 20, 2004 12:42 pm
Location: Santa Monica
Contact:

regex prob

Post 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.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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);
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

fully aware of that

I was considering he would be adding more paramaters?
if not, the above code is all u need.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

adding more to each of those arrays will still work and execute each one.
eNtropicChild
Forum Newbie
Posts: 2
Joined: Tue Apr 20, 2004 12:42 pm
Location: Santa Monica
Contact:

Post 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&#1111;$i],$match);
		$match = ereg_replace("\n"," -- ",$match&#1111;0]);
		$match = ereg_replace("st1nk1","",$match);
		$match = ereg_replace("st1nk2","",$match);
		$boom&#1111;$i] = preg_replace("/st1nk1(.+?)st1nk2/s",$match,$boom&#1111;$i]);
Post Reply