Connecting Tech Pros Worldwide Forums | Help | Site Map

String Replace

RageARC
Guest
 
Posts: n/a
#1: Sep 24 '07
I have:

$string = "text ... [COLOR]FFFFFF[/COLOR] text... ";
$replacement = "<div id='THECOLOR'>THECOLOR</div>";

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?


burgermeister01@gmail.com
Guest
 
Posts: n/a
#2: Sep 24 '07

re: String Replace


On Sep 24, 12:54 pm, RageARC <rage...@gmail.comwrote:
Quote:
I have:
>
$string = "text ... [COLOR]FFFFFF[/COLOR] text... ";
$replacement = "<div id='THECOLOR'>THECOLOR</div>";
>
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\]", "<div id = '" . $color .
"'>" . $color . "</div>", $string);

RageARC
Guest
 
Posts: n/a
#3: Sep 24 '07

re: String Replace


This is untested but try this out...
Quote:
>
preg_replace("\[COLOR\][a-zA-Z]+\[/COLOR\]", "<div id = '" . $color .
"'>" . $color . "</div>", $string);
Where did you get the $color variable?

gosha bine
Guest
 
Posts: n/a
#4: Sep 24 '07

re: String Replace


RageARC wrote:
Quote:
I have:
>
$string = "text ... [COLOR]FFFFFF[/COLOR] text... ";
$replacement = "<div id='THECOLOR'>THECOLOR</div>";
>
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\]~',
"<div id='$1'>$1</div>",
$string);


--
gosha bine

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