I have no clue how to validate this string. I am simply supplying an IV for an encryption, but can find no 1is_hex()1 or similar function, I can’t wrap my head around it! I read on a comment in the php documentation (user contrib. notes) this:
if($iv == dechex(hexdec($iv))) { //True } else { //False }
But that doesn't seem to work at all.. It only says false. If it helps my input of my IV would be this:
92bff433cc639a6d
PHP | ctype_xdigit() Function The ctype_xdigit() function in PHP used to check each and every character of string/text are hexadecimal digit or not. It return TRUE if all characters are hexadecimal otherwise return FALSE .
The length of the hexadecimal color code should be either 6 or 3, excluding '#' symbol. For example: #abc, #ABC, #000, #FFF, #000000, #FF0000, #00FF00, #0000FF, #FFFFFF are all valid Hexadecimal color codes.
Yes, in hexadecimal, things like A, B, C, D, E, and F are considered numbers, not letters. That means that 200 is a perfectly valid hexadecimal number just as much as 2FA is also a valid hex number.
Use function : ctype_xdigit
<?php $strings = array('AB10BC99', 'AR1012', 'ab12bc99'); foreach ($strings as $testcase) { if (ctype_xdigit($testcase)) { echo "The string $testcase consists of all hexadecimal digits.\n"; } else { echo "The string $testcase does not consist of all hexadecimal digits.\n"; } } ?>
The above example will output:
AB10BC99
consists of all hexadecimal digits.AR1012
does not consist of all hexadecimal digits.ab12bc99
consists of all hexadecimal digits.Another way without ctype
or regex
:
$str = 'string to check'; if (trim($str, '0..9A..Fa..f') == '') { // string is hexadecimal }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With