eddiefantastic Posted October 9, 2007 Share Posted October 9, 2007 Hi. I'm a bit stumped. I'm trying to convert a colour code from one format to another. basically convert something like: 0xFF301E10xE9FF1E230x28FF414 to: <font color="#FF301E">1</font><font color="#E9FF1E">23</font><font color="#28FF41">4</font> I've started the function: function colourize($string){ $string = str_replace('0x', '<font color="#', $string); return $string; } But that is obviously wrong. what it needs to do: 1. find an instance of '0x' 2. replace that with '<font color="#' 3. count 6 characters along, insert '">' 4. insert all chars into the new string till it reaches '0x', insert '</font>' before this and continue. or something. This is beyond my programming knowhow. Any help appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/72450-solved-replace-parts-of-string/ Share on other sites More sharing options...
haaglin Posted October 9, 2007 Share Posted October 9, 2007 Is this what you want? <?php function colourize($string){ foreach(explode("0x",$string) as $code) { if(empty($code)) continue; $str.= '<font color="'.substr($code,0,6).'">'.substr($code,6,7).'</font>'; } return $str; } echo colourize("0xFF301E10xE9FF1E230x28FF414"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/72450-solved-replace-parts-of-string/#findComment-365341 Share on other sites More sharing options...
eddiefantastic Posted October 9, 2007 Author Share Posted October 9, 2007 With the addition of the "#" symbol, it was perfect. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/72450-solved-replace-parts-of-string/#findComment-365360 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.