note 95888 deleted from function.hexdec by danbrown
| From: | danbrown@php.net | Date: | Wed, 27 Jan 2010 14:34:54 +0000 |
| Subject: | note 95888 deleted from function.hexdec by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-165367@lists.php.net to get a copy of this message | ||
Note Submitter: ajim1417 at gmail dot com
----
Thanks Halit YEL for your useful rgb2hex2rgb function.
Actually, you had a typo there, in line 20.
It should typed like this :
$e = explode(":", $spr);
instead of
$e = explode(":", $c);
because if it explode ":" from $c, it will fails because there are no ":" on $c
string.
And also, when I ran this function in PHP 5.3, it will display a deprecated warning because you use
eregi. Instead of eregi you could use preg_match. Here is the simple fix for the line that contain
eregi function :
if(preg_match("/^[0-9ABCDEFabcdef\#]+$/i", $c)){
and
}elseif (preg_match("/^[0-9]+(,| |.)+[0-9]+(,| |.)+[0-9]+$/i", $c)){
So if all combined together (with the fix), it will become :
<?PHP
function rgb2hex2rgb($c){
if(!$c) return false;
$c = trim($c);
$out = false;
if(preg_match("/^[0-9ABCDEFabcdef\#]+$/i", $c)){
$c = str_replace('#','', $c);
$l = strlen($c) == 3 ? 1 : (strlen($c) == 6 ? 2 : false);
if($l){
unset($out);
$out[0] = $out['r'] = $out['red'] = hexdec(substr($c, 0,1*$l));
$out[1] = $out['g'] = $out['green'] = hexdec(substr($c, 1*$l,1*$l));
$out[2] = $out['b'] = $out['blue'] = hexdec(substr($c, 2*$l,1*$l));
}else $out = false;
}elseif (preg_match("/^[0-9]+(,| |.)+[0-9]+(,| |.)+[0-9]+$/i", $c)){
$spr = str_replace(array(',',' ','.'), ':', $c);
$e = explode(":", $spr);
if(count($e) != 3) return false;
$out = '#';
for($i = 0; $i<3; $i++)
$e[$i] = dechex(($e[$i] <= 0)?0:(($e[$i] >= 255)?255:$e[$i]));
for($i = 0; $i<3; $i++)
$out .= ((strlen($e[$i]) < 2)?'0':'').$e[$i];
$out = strtoupper($out);
}else $out = false;
return $out;
}
?>
Anyway, thanks for this.