pootsey Posted August 23, 2008 Share Posted August 23, 2008 Right guys, basically what I'm trying to do is seperate a string ie $string = "onetwothreefourfive" and then apply two repeating html codes to each, ie one would get blue, two would get red, three would get blue, four would get red and so on. Any idea how to do this? thanks! Quote Link to comment Share on other sites More sharing options...
marcus Posted August 23, 2008 Share Posted August 23, 2008 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); ?> Quote Link to comment Share on other sites More sharing options...
pootsey Posted August 23, 2008 Author Share Posted August 23, 2008 you my friend are a genius, thanks! Quote Link to comment Share on other sites More sharing options...
pootsey Posted August 23, 2008 Author Share Posted August 23, 2008 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? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 23, 2008 Share Posted August 23, 2008 Like this? $color = "red"; //start at red? while($row = mysql_fetch_array($result)){ //I'm assuming you are getting it from a database... echo "The color is {$color}.<br />"; $color = $color == "red" ? "blue" : "red"; } Quote Link to comment Share on other sites More sharing options...
marcus Posted August 23, 2008 Share Posted August 23, 2008 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 Quote Link to comment Share on other sites More sharing options...
pootsey Posted August 23, 2008 Author Share Posted August 23, 2008 mgallforever that's exactly what I want, although that appears to only work for numbers? if I change $data = range (1,50) to something like $data = "hello" then it wont work. Quote Link to comment Share on other sites More sharing options...
marcus Posted August 23, 2008 Share Posted August 23, 2008 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++; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.