Jump to content

[SOLVED] replace parts of string


eddiefantastic

Recommended Posts

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/72450-solved-replace-parts-of-string/
Share on other sites

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");
?>

 

 

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.