note 95888 added to function.hexdec
| From: | ajim1417atgmaildotcom at osu1 dot php dot net | Date: | Wed, 27 Jan 2010 12:06:14 +0000 |
| Subject: | note 95888 added to function.hexdec | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-165363@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 202.158.52.162
Probable Submitter: 125.167.44.67
----
Manual Page -- http://www.php.net/manual/en/function.hexdec.php
Edit -- https://master.php.net/note/edit/95888
Del: integrated -- https://master.php.net/note/delete/95888/integrated
Del: useless -- https://master.php.net/note/delete/95888/useless
Del: bad code -- https://master.php.net/note/delete/95888/bad+code
Del: spam -- https://master.php.net/note/delete/95888/spam
Del: non-english -- https://master.php.net/note/delete/95888/non-english
Del: in docs -- https://master.php.net/note/delete/95888/in+docs
Del: other reasons-- https://master.php.net/note/delete/95888
Reject -- https://master.php.net/note/reject/95888
Search -- https://master.php.net/manage/user-notes.php