[OT]PHP and regular expressions

Liquide

DEX: 4
Well I need some PHP help and if there are someone that is good at this and can help me out I would become very happy :)

I have a string in an array that is called $content[TheDesc] in this string I want to search for a specific pattern and pass it along to the function dopicencode().

The pattern is [IMGCODE]string[/IMGCODE] (observe that the IMGCODE and /IMGCODE is enclosed in brackets inside the $content[TheDesc]). The string that is enclosed in [IMGCODE] and [/IMGCODE] I want to send along to the dopicencode() function.

The result of the dopicencode() I want to return and use as a replacement of the whole [IMGCODE]string[/IMGCODE] pattern.

Anyone that knows enough of PHP to help me out with this :)
 

log in or register to remove this ad

The code I tried that didn't work

OK I'm fairly new to PHP and this is what I tried at first but it just returns a foobar result.

$imageTemp = array ("[IMGCODE]","[/IMGCODE]");
$replaceTemp = array("","");
$strTemp = str_replace($imageTemp,dopicencode($replacetemp),$content[TheDesc]);

So if you can help me out, HEEEEELP! :) I need it
 



Code:
[color="#FFFFFF"]

$content[TheDesc] = "bblah blah hahahah [IMGCODE]Liquide[/IMGCODE] i roxor";

$ImgCodeBegin = "[IMGCODE]";
$ImgCodeEnd = "[/IMGCODE]";

if(strstr($content[TheDesc],$ImgCodeBegin))
{
	$PosBegin = strpos($content[TheDesc], $ImgCodeBegin) + strlen($ImgCodeBegin);
	$PosEnd = strrpos($content[TheDesc], $ImgCodeEnd);
	$ChunkedString = substr($content[TheDesc], $PosBegin, ($PosEnd - $PosBegin));

	echo $ChunkedString;

	//dopicencode($ChunkedString);
}
[/color]

Some comments in Swedish, I'm pretty damn tired :)

Ovanståande fungerar, men enbart på en bild per sträng. DEt fungerar inte så bra om $content[TheDesc] = "bblah blah hahahah [IMGCODE]Liquide[/IMGCODE] i roxor [IMGCODE]Bild2[/IMGCODE]";
exempelvis.

Du kan ju experimentera lite med det. Jag hatar stränghantering i PHP. :)
 
Last edited:

Psionicist said:
Code:
[color="#FFFFFF"]

$content[TheDesc] = "bblah blah hahahah [IMGCODE]Liquide[/IMGCODE] i roxor";

$ImgCodeBegin = "[IMGCODE]";
$ImgCodeEnd = "[/IMGCODE]";

if(strstr($content[TheDesc],$ImgCodeBegin))
{
	$PosBegin = strpos($content[TheDesc], $ImgCodeBegin) + strlen($ImgCodeBegin);
	$PosEnd = strrpos($content[TheDesc], $ImgCodeEnd);
	$ChunkedString = substr($content[TheDesc], $PosBegin, ($PosEnd - $PosBegin));

	echo $ChunkedString;

	//dopicencode($ChunkedString);
}
[/color]

Hmm. So PHP is basically C with dollar signs. Am I correct?

Anyways, I always thought PHP was that funky encryption thingamajig thought up by Phil Zimmerman (he of PKZIP).


Hong "or was that PCP?" Ooi
 
Last edited:

Better living through regular expressions

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.
 

hong said:
Hmm. So PHP is basically C with dollar signs. Am I correct?

Only to the extent that Java is C with classes.

PHP is pretty much a stripped down, web-focused Perl (or at least that's what a looks like to someone with minimal exposure to PHP; I do my web programming in ASP.NET/VB.NET).
 

Re: Better living through regular expressions

Scarab said:

Code:
[color=white]
$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];

[/color]

Wow. If you look at that code reeeaaally closely, it starts to rotate!


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

I think you are confused, "Scarab", if that is your real name. Everyone knows PGP is a drug that people serve at parties. Do not confuse PGP with PCP, which is Phil Catz's Privacy program, or with PNP, the plug and play protocol used by ISO-certified modems.

You should be glad I'm here to correct your grevious mistakes. In the future, please check your facts before posting nonsense to UNsenet bboards. I always do.

HTH!
 


Remove ads

Top