Page 1 of 1

Removing different sets of characters in a string

Posted: Thu Aug 20, 2009 3:52 pm
by psionic98
Hi all, I'm new to php coding and have a question.

I have a database that has some fields which contain our version of color codes. The codes are as follow:
&G=bright green
&g=dark green
&n=new line
&R=bright red
&r=dark red

and so on and so on through the normal spectrum.

my problem is trying to remove those codes from the string before printing it out.

I've looked at explode/preg_replace, but cant seem to iron this out.. an example of a string would be:

&GI jumped &Yover &Gthe fence&n

I need it to print out as "I jumped over the fence" without the quotes obviously!

Any help is appreciated... I was thinking to put all of the color codes into an array, but I don't know how to go about checking against the array to remove the code.

Thanks

stan

Re: Removing different sets of characters in a string

Posted: Thu Aug 20, 2009 4:23 pm
by cpetercarter
Something like this, perhaps:

Code: Select all

$codes = array('&r', '&g', '&b', '&y');
 
foreach ($codes as $code)  {
$text = str_replace($code,"",$text);
}
 

Re: Removing different sets of characters in a string

Posted: Thu Aug 20, 2009 4:58 pm
by jackpf
You don't need the foreach loop since str_replace() accepts arrays as parameters.

Re: Removing different sets of characters in a string

Posted: Fri Aug 21, 2009 9:17 am
by psionic98
worked great and didnt need the for loop.. thanks a bunch