Page 1 of 1

regular expression

Posted: Fri Nov 10, 2006 9:01 am
by mcog_esteban
Hello.

What regular expression should i use to turn a string like "^1String 1^0String2" into

Code: Select all

<span style="color : red;">String 1</span><span style="color : black;">String 2</span>
Thanks in advice.

Posted: Fri Nov 10, 2006 9:51 am
by pickle
What have you tried so far? If you're not sure about regex, search the forums for ~d11wtq's Regex tutorials.

Moving to Regex forum.

Posted: Fri Nov 10, 2006 10:03 am
by mcog_esteban
i have done this:

Code: Select all

$find = array('^1', '^0');
$replace = array(' <span style="color : red;" ', '<span style="color : red;" ');

$new = str_replace($find, $replace, "^1String1^0String2";
problem is closing the span tag.
that why i need i think a regular expression works better than this.
i'm not familiarized with expressions.

Posted: Fri Nov 10, 2006 10:07 am
by pickle
Go have a look at the tutorials - that should give you a good start.

Posted: Mon Nov 13, 2006 5:45 am
by mcog_esteban
Hello.
I have done this and it's working good.

Code: Select all

<?php

$string ="^1String1^2String2^3String3";

$colors= array(1 => "red" ,2 => "blue" ,3 => "black");

foreach($colors as $key => $value)
{
	$string = preg_replace("/\^($key)([^\^]*)/", "<span style=\"color : $value;\">$2</span>", $string);
}
?>
Now, does anyone thinks this can be done in a simple or faster way?
Thank you.

Posted: Tue Nov 14, 2006 9:53 am
by pickle
This is about as simple as you can get it I think.

Posted: Tue Nov 14, 2006 10:08 am
by mcog_esteban
hey but i found something i don't know if i can call it a problem..

Take this two strings:

$string ="^1String1^2String2^3String3^4String4";
$string2 ="^1String1^2String2^1String3^2String4";

They print ok on the browser, but if i look at the source:

Ok html code.

Code: Select all

<span style="color : red;">String1</span><span style="color : green;">String2</span><span style="color : blue;">String3</span><span style="color : black;">String4</span>
Missing </span>, multiple </span> tags together

Code: Select all

<span style="color : red;">String1</span><span style="color : green;">String2<span style="color : red;">String3</span></span><span style="color : green;">String4</span>
it seems if i have the same color on the string it produces an erractic behaviour.
how can i prevent this?
thanks.

Posted: Tue Nov 14, 2006 1:16 pm
by Chris Corbyn

Code: Select all

<?php

$string = "^1String1^2String2^1String3^2String4";

$keys = array(1 => 'red', 2 => 'black');

$newstring = $string;
$loopnum = 0;
foreach ($keys as $k => $v)
{
    $newstring = preg_replace("/\\^" . $k . "([^\\^]+)/", "<span style=\"color:" . $v . "\">\$1</span>", $newstring);
	echo (++$loopnum) . ") => " . $newstring . "\n";
}

echo "Result) => " . $newstring;
Outputs (Yeah I know it's wrong but it should be obvious why):

Code: Select all

1) => <span style="color:red">String1</span>^2String2<span style="color:red">String3</span>^2String4
2) => <span style="color:red">String1</span><span style="color:black">String2<span style="color:red">String3</span></span><span style="color:black">String4</span>
Result) => <span style="color:red">String1</span><span style="color:black">String2<span style="color:red">String3</span></span><span style="color:black">String4</span>
It's removing the carets so it has nowhere to stop.

How about this?

Code: Select all

<?php

$string = "^1String1^2String2^1String3^2String4";

$keys = array(1 => 'red', 2 => 'black');

$newstring = $string;
foreach ($keys as $k => $v)
{
    $newstring = preg_replace("/\\^" . $k . "([^\\^]+)/", "^" . $k . "<span style=\"color:" . $v . "\">\$1</span>", $newstring);
}
$newstring = preg_replace("/\\^[0-9]/", "", $newstring);

echo $newstring;

Posted: Tue Nov 14, 2006 1:23 pm
by Chris Corbyn
Actually, this is much cleaner:

Code: Select all

$string = "^1String1^2String2^1String3^2String4";

function color($matches)
{
	$keys = array(1 => 'red', 2 => 'black');
	return "<span style=\"color:" . $keys[$matches[1]] . "\">". $matches[2] ."</span>";
}

$newstring = preg_replace_callback("/\\^(\\d)([^\\^]+)/", "color", $string);

echo $newstring;
EDIT | Or even...

Code: Select all

$string = "^1String1^2String2^1String3^2String4";

$keys = array(1 => 'red', 2 => 'black');
$newstring = preg_replace("/\\^(\\d)([^\\^]+)/e", '"<span style=\"color:$keys[$1]\">$2</span>"', $string);

Posted: Tue Nov 14, 2006 3:34 pm
by mcog_esteban
Thanks a lot d11wtq.