Jump to content

preg_replace?


pootsey

Recommended Posts

I'm not so good at regex, but you could just use str replace

 

<?php
$string = "onetwothreefourfive";

$array = array("one","two","three","four","five");
function color($c,$txt){ return "<font color=\"".$c."\">".$txt."</font>";  }
$repl = array(color('blue','one'),color('red','two'),color('blue','three'),color('red','four'),color('blue','five'));

echo str_replace($array,$repl,$string);
?>

Link to comment
https://forums.phpfreaks.com/topic/120957-preg_replace/#findComment-623565
Share on other sites

ah actually, just realised something with it, the colours dont repeat, my one twothreefourfive was a bad example.

 

What I actually meant was for example the string would be a value (a username to be specific) pulled from a database which I want coloured like above.

 

So it could be any amount of characters but i still want it to be coloured like that.

 

Anyway of that happening?

Link to comment
https://forums.phpfreaks.com/topic/120957-preg_replace/#findComment-623575
Share on other sites

hmm, like just repeating red and blue?

 

<?php
$data = range(1,50);

function color($c,$txt){ return "<font color=\"".$c."\">".$txt."</font>";  }

$x = 0;
foreach($data AS $lemons){
if($x % 2){
	echo color('red',$lemons) . " ";
}else {
	echo color('blue',$lemons) . " ";
}

$x++;
}
?>

 

results: http://i34.tinypic.com/wungvb.jpg

Link to comment
https://forums.phpfreaks.com/topic/120957-preg_replace/#findComment-623580
Share on other sites

No, you could do something like this.

 

<?php
function color($c,$txt){ return "<font color=\"".$c."\">".$txt."</font>";  }

$sql = "SELECT * FROM `users` ORDER BY username ASC";
$res = mysql_query($sql) or die(mysql_error());

$x = 0;
while($row = mysql_fetch_assoc($res)){
if($x % 2){
	echo color('red',$row['username']) . " ";
}else {
	echo color('blue',$row['username']) . " ";
}

$x++;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/120957-preg_replace/#findComment-623590
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.