Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Need solution or description of: How to convert color value from ARGB to RGBA which is possible to use for css:
Examples:
ARGB color : #ff502797 convert to RGBA
RGBA result example: rgba(80,39,151,1)
thx.
ARGB contains 4 groups of HEX values(channels): Alpha, Red, Green, Blue.
Scheme:
Exampe: #ff502797 - color in ARGB formatt
Elements on positions:
0,1 - Alpa (ff)
2,3 - Red (50)
4,5 - Green (27)
6,7 - Blue (97)
After this convert each channel to decimal. And putt on their places into RBGA:
rgba(Red,Green,Blue,Alpha) - rgba(80,39,151,1)
Example function:
function argb2rgba($color)
{
$output = 'rgba(0,0,0,1)';
if (empty($color))
return $output;
if ($color[0] == '#') {
$color = substr($color, 1);
}
if (strlen($color) == 8) { //ARGB
$opacity = round(hexdec($color[0].$color[1]) / 255, 2);
$hex = array($color[2].$color[3], $color[4].$color[5], $color[6].$color[7]);
$rgb = array_map('hexdec', $hex);
$output = 'rgba(' . implode(",", $rgb) . ',' . $opacity . ')';
}
return $output;
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
How can I format a string to prepend UTC if it starts with + or - and add a : before the last two digits if it ends with 4 digits?
Examples and expected result:
PST > PST
+08 > UTC+08
-0845 > UTC-08:45
Thank you!
<?php
$string = "PST";
if (substr($string, 0, 1) === '+' || substr($string, 0, 1) === '-'){
if(strlen($string) == 3){
$newString = 'UTC'.$string;
}
else{
$newString = 'UTC'. substr($string, 0, 3). ':' .substr($string, -2, 2);
}
}
else{
$newString = $string;
}
echo $newString;
You don't need regex for that or at least regex seems overkill. You can simple create this logic using an if-else and modify your string accordingly
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
function inchesTodecimal($inches){
return $decimal=inches/12;
}
I want convert inches to decimal like 11=0.917. I want to save 11 inches in database like decimal number 0.917.there is a problem occur when inches is 10.In float type .10=1
First you convert your feet Inches in to feet decimal like 5 feet and after point value convert it into decimal like 11=9.17 then insert into your database. When you want to show on user end you convert again feet Decimal to feet Inches like 5.917=>5.11 its simple hope you got what you want...
Here my simple code for conversion:
function feetInchesToDecimal($feetinches,$precision = 3){
list($feet,$inches) = explode(".",$feetinches);
$inches = preg_replace('/[^0-9.]/','',$inches);
$inches = $inches + ($feet * 12);
return round($inches /12,$precision);
}
function feetDecimalToInchs($feetdecimal){
$feet=(int)$feetdecimal;
$inches=round(($feetdecimal-$feet)*12);
$feetinches=$feet;
if($inches>0) $feetinches.=".".$inches;
return $feetinches;
}
echo feetInchesToDecimal(5.11); //5.917
echo feetDecimalToInchs(5.917); //5.11
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am just trying to figure out how I can get the actual digits of a figure that has been calculated within php and formatted to have 2 decimal places.
so say its calculated it to be 45.76 I am trying to figure out how I can get the 76 from it for an if statement. Basically I want it to look and just say that if it's 00 then remove them, if not, show them.
Thanks
Try :
function showDecimals($v){
$n = abs($v);
$whole = floor($n);
$fraction = $n - $whole;
return $fraction > 0
}
And...
if (showDecimals(10.15)){
//Show
}else{
//Remove?
}
You want to show a whole number if there is no decimal place, and 2 decimals of precision if not?
Method 1
function formatNumber($n) {
$n = round($n*100)/100;
return ''+$n;
}
This simply rounds it to 2 decimals of precision. Zero truncation is automatic.
Usage
echo formatNumber(0); //0
echo formatNumber(0.5); //0.5
echo formatNumber(0.894); //0.89
echo formatNumber(0.896); //0.9
echo formatNumber(1.896); //1.9
Method 2
Or if you 1.9 to display as 1.90, I suppose this would work:
function formatNumber($n) {
if ($n == 0)
return ''+$n;
$str = ''.round($n*100)/100;
$dotpos = strrpos('.', $str);
if (strlen(substr($str, $dotpos+1)) === 2)
$str .= '0';
return $str;
}
Usage:
echo formatNumber(0); //0
echo formatNumber(0.5); //0.50
echo formatNumber(0.894); //0.89
echo formatNumber(0.896); //0.90
echo formatNumber(1.896); //1.90
Edit: Accidentally posted broken version of method 2, should be fixed now.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have generate a 16 digit number and i want to extract one number from every 4 digits of that 16 digit number. For e.g: 1234567892345678. I want to extract 2 from 1234, 7 from 5678, 3 from 9034 & 7 from 5678. Then store it in another variable $a. the extraction will be in a random manner.
You can try this -
$d = '1234567892345678';
$s = str_split($d, 4); // split in 4 digits
$n = array_map(function($x) {
return substr($x, rand(0, 3), rand(1, 1)); // extract single digit random number
}, $s);
$n will hold the random numbers.
I might be late at answering this question but you can simply use strlen function along with for loop like as
$str = "1234567892345678";
for($i = 0; $i < strlen($str);$i += 4){
echo $str[$i+rand(0,3)];
}
Here you have a one-liner:
$s = $string[rand(0,3)].$string[rand(4,7)].$string[rand(8,11)].$string[rand(12,15)];
echo $s;
Another one-liner:
for($s='',$i=0;$i<10; $s.=$string[rand($i,$i+=3)]);
echo $s;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is it possible to convert rgba color code to hex or rgb equivalent color code in php. I have seared a lot but I found some js functions but not in php.
Please help
When there are sources in JavaScript it should not be a problem to migrate the code into PHP...
RGB to HEX (considering we have our R, G, B colors in $r, $g, $b variables):
function toHex($n) {
$n = intval($n);
if (!$n)
return '00';
$n = max(0, min($n, 255)); // make sure the $n is not bigger than 255 and not less than 0
$index1 = (int) ($n - ($n % 16)) / 16;
$index2 = (int) $n % 16;
return substr("0123456789ABCDEF", $index1, 1)
. substr("0123456789ABCDEF", $index2, 1);
}
echo $hex = '#' . toHex($r) . toHex($g) . toHex($b);
Didn't tested but should be working. Should You need RGBa -> RGB conversion desirabily, let me know...