3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function swift_rgb2hex($rgb,$hash=true) { if($rgb == NULL){ return 'none'; } if($hash) $hex = '#'; if(strpos($rgb,'rgb') === false){ return '#'.$rgb; } $temp = explode('(',$rgb); $rgb = $temp[1]; $temp = explode(')',$rgb); $rgb = $temp[0]; $rgb = explode(',',$rgb); $hex .= str_pad(dechex($rgb[0]), 2, "0", STR_PAD_LEFT); $hex .= str_pad(dechex($rgb[1]), 2, "0", STR_PAD_LEFT); $hex .= str_pad(dechex($rgb[2]), 2, "0", STR_PAD_LEFT); return $hex; } echo swift_rgb2hex('rgba(22,22,22,1)'); echo swift_rgb2hex('FFFFFF'); echo swift_rgb2hex('bamam'); echo swift_rgb2hex('rgba(255,255,0,1)');
Output for 4.3.0 - 7.1.0
#161616#FFFFFF#bamam#ffff00