regular expression

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
mcog_esteban
Forum Contributor
Posts: 127
Joined: Tue Dec 30, 2003 3:28 pm

regular expression

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
mcog_esteban
Forum Contributor
Posts: 127
Joined: Tue Dec 30, 2003 3:28 pm

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Go have a look at the tutorials - that should give you a good start.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
mcog_esteban
Forum Contributor
Posts: 127
Joined: Tue Dec 30, 2003 3:28 pm

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

This is about as simple as you can get it I think.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
mcog_esteban
Forum Contributor
Posts: 127
Joined: Tue Dec 30, 2003 3:28 pm

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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;
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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);
mcog_esteban
Forum Contributor
Posts: 127
Joined: Tue Dec 30, 2003 3:28 pm

Post by mcog_esteban »

Thanks a lot d11wtq.
Post Reply