[OT]PHP and regular expressions

Re: Better living through regular expressions

Scarab said:
Oh my. That's an awful lot of code for something that can be done with two quick regular expressions. And ugly C-style code at that.

Well. Let me give it a shot and show you how it's done the PHP way. :D

Code:
$content[TheDesc] = <whatever your content is>;

// Regular expression to get all occurances of "[IMGCODE].*[/IMGCODE]"
preg_match_all("/\[IMGCODE\](.*)\[\/IMGCODE\]/", $content[TheDesc], $contentmatch);

// Loop through result array and perform replace
foreach($contentmatch[1] as $replace) {
	$content[TheDesc] = preg_replace("/\[IMGCODE\](.*)\[\/IMGCODE\]/", dopicencode($replace), $content[TheDesc], 1);
}

// Done!

return $content[TheDesc];

I haven't tried it, but it should work. Or at least give you a good start. Better living through regular expressions! Impress your friends with a regular expression party trick! Defeat your enemies with your regular expression kung fu! Regular expressions make your teeth shiny and improves your sex life!

Crap. Bostream is giving me problems right now. Well, I'll post as soon as it's up and running.

Hong: That's PGP you're thinking about.

Yes, I and Liquide did something like that moments later I posted the above "solution".
 

log in or register to remove this ad

Ack...someone beat me to it. Then again, I did spend the last five hours trying to figure out why my computer kept powering off and losing CMOS settings...

Oh, well. *bows to Scarab*
 


Remove ads

Top