# [OT]PHP and regular expressions



## Liquide (Oct 21, 2002)

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


----------



## Liquide (Oct 21, 2002)

*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


----------



## Mathew_Freeman (Oct 21, 2002)

This should be in meta, of course.


----------



## Liquide (Oct 21, 2002)

my bad, I hope a mod can move it along


----------



## Psionicist (Oct 21, 2002)

```
[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.


----------



## hong (Oct 21, 2002)

Psionicist said:
			
		

> ```
> [color="#FFFFFF"]
> 
> $content[TheDesc] = "bblah blah hahahah [IMGCODE]Liquide[/IMGCODE] i roxor";
> ...




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


----------



## Scarab (Oct 22, 2002)

*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. 


```
$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.


----------



## drothgery (Oct 22, 2002)

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).


----------



## hong (Oct 22, 2002)

*Re: Better living through regular expressions*



			
				Scarab said:
			
		

> *
> 
> 
> Code:
> ...




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!


----------



## Baron Von StarBlade (Oct 22, 2002)

Hong has temporarily dethroned PirateCat as my hero.


----------



## Psionicist (Oct 22, 2002)

*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.
> 
> ...




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


----------



## Dakhran the Dark (Oct 23, 2002)

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*


----------



## MeepoTheMighty (Oct 23, 2002)

The boards are always best when hong's drunk.


----------

