String Replace

String Replace

am 24.09.2007 19:54:37 von ragearc

I have:

$string = "text ... [COLOR]FFFFFF[/COLOR] text... ";
$replacement = "

THECOLOR
";

Where THECOLOR has to be the color the user inserted inside the tags.
I could use preg_replace, but I don't know regex. Can you help me?

Re: String Replace

am 24.09.2007 20:07:46 von burgermeister01

On Sep 24, 12:54 pm, RageARC wrote:
> I have:
>
> $string = "text ... [COLOR]FFFFFF[/COLOR] text... ";
> $replacement = "

THECOLOR
";
>
> Where THECOLOR has to be the color the user inserted inside the tags.
> I could use preg_replace, but I don't know regex. Can you help me?


This is untested but try this out...

preg_replace("\[COLOR\][a-zA-Z]+\[/COLOR\]", "
" . $color . "
", $string);

Re: String Replace

am 24.09.2007 20:15:44 von gosha bine

RageARC wrote:
> I have:
>
> $string = "text ... [COLOR]FFFFFF[/COLOR] text... ";
> $replacement = "

THECOLOR
";
>
> Where THECOLOR has to be the color the user inserted inside the tags.
> I could use preg_replace, but I don't know regex. Can you help me?
>


try

preg_replace('~\[COLOR\](.+?)\[/COLOR\]~',
"
$1
",
$string);


--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok

Re: String Replace

am 24.09.2007 20:15:48 von ragearc

> This is untested but try this out...
>
> preg_replace("\[COLOR\][a-zA-Z]+\[/COLOR\]", "

" . $color . "
", $string);

Where did you get the $color variable?