Search code examples
phpcolorshexcolor-picker

PHP: Convert hex color from color input to PHP hex number


I want to convert my hex color from a color input (example: #FFFFFF) to a PHP hex number format (example: 0xFFFFFF).

i tried to replace the # with 0x by using str_replace but this converted it to a string, I want to keep it a number.

Any solution?


Solution

  • Perhaps this function is what you need?

    http://www.php.net/manual/en/function.hexdec.php

    "Returns the decimal equivalent of the hexadecimal number represented by the hex_string argument. hexdec() converts a hexadecimal string to a decimal number.

    hexdec() will ignore any non-hexadecimal characters it encounters."

    Some added info:

    dechex() will give you a string containing a hex representation of the number if you need it, but it seems you should be dealing with an integer if it's a number that you need to pass around.