1

Supposing I have a hex color code in a string such as:

$color = '#fda80d';

How could I convert this into an actual hex value such that:

$color = 0xfda80d;

So that it is not a string but an actual hex value?

btantlinger
  • 803
  • 8
  • 11

5 Answers5

2
$color = '#fda80d';
$color = hexdec(substr($color, 1));

// Prove it
echo $color == 0xfda80d ? "yes" : "failed";
flowfree
  • 16,356
  • 12
  • 52
  • 76
  • This will also pass a `===` which is interesting. I guess PHP converts hex numbers to dec for type comparison too. Maybe it is seen as just a number instead of a certain number type? – afuzzyllama May 22 '12 at 18:24
  • 1
    Actually the result is an int, 16623629. But both `$color === 16623629` and `$color === 0xfda80d` are the same thing. – flowfree May 22 '12 at 18:28
1

The below code shows that PHP stores numbers as base 10 but you can do math on the number in any base. I've created a demo below that should give you a better idea of how you can use this to compare numbers in different bases against each other. You can also do math on numbers in different bases:

<?php
$color = '#fda80d'; //String color
$color = substr($color,1,6); //String color without the #

$color = '0x'.$color; //Prepend the 0x
$color += 0x00; //Force it to be a number

if($color === 0xfda80d) //Check the number is EXACTLY what we are expecting
    echo 'Is hex<br />'; //This code is run, proving it is a hex ***number***

echo $color; //Outputs 16623629 (base 10)

#================================#

echo '<br /><br />';

$octal = 011; //Define an octal number
$octal += 01; //Add 1

if($octal === 012)
    echo 'Is octal<br />';

echo $octal; //Outputs 10 (base 10)

#================================#

echo '<br /><br />';

$number = 12345; //Define a standard base 10 int
$number += 1; //Add 1 to the int

if($number === 12346)
    echo 'Is base 10<br />';

echo $number; //Outputs 12346 (base 10)


#================================#

echo '<br /><br />';

if($color === 16623629 && $color === 077324015 && $color === 0xFDA80D)
    echo '$color is base 10, octal and hex all at the same time<br />';

if($octal === 10 && $octal === 012 && $octal === 0xA)
    echo '$octal is base 10, octal and hex all at the same time<br />';

if($number === 12346 && $number === 030072 && $number === 0x303A)
    echo '$number is base 10, octal and hex all at the same time';
?>

Outputs:

Is hex

16623629

Is octal

10

Is base 10

12346

$color is base 10, octal and hex all at the same time

$octal is base 10, octal and hex all at the same time

$number is base 10, octal and hex all at the same time

You can check a base 10 number (or hex/oct), for example 10, is equal to the hex number 0xA on the fly without any conversion:

<?php
if(10 === 0xA)
    echo 'Success'; //This code is run
?>
OdinX
  • 4,135
  • 1
  • 24
  • 33
0

You can look into PHP's bin2hex(). http://php.net/manual/en/function.bin2hex.php

Caleb Taylor
  • 177
  • 9
0

var_dump(dechex(hexdec(str_replace('#', '', '#fda80d')))) is a string, so probably there is no way you can get 0xfda80d like you wrote it.

Dejan Marjanović
  • 19,244
  • 7
  • 52
  • 66
0

Another option is to use the handy sscanf() function.

sscanf($color, '#%06x', $color);
salathe
  • 51,324
  • 12
  • 104
  • 132