I have no clue how to validate this string. I am simply supplying an IV for an encryption, but can find no "is_hex()" 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

Accepted Answer

use function : ctype_xdigit

http://php.net/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:

The string AB10BC99 consists of all hexadecimal digits.

The string AR1012 does not consist of all hexadecimal digits.

The string ab12bc99 consists of all hexadecimal digits.

Written by Haim Evgi
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki