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

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.