PHP:简单,如果字符串是十六进制验证?

oni*_*kun 26 php encryption binary hex

我不知道如何验证这个字符串.我只是提供一个加密的IV,但是找不到1is_hex()1或类似的功能,我无法绕过它!我在php文档(用户贡献.注释)中阅读了一条评论:

if($iv == dechex(hexdec($iv))) {
  //True
} else {
  //False
}
Run Code Online (Sandbox Code Playgroud)

但这似乎根本不起作用......它只是说错误.如果它有助于我输入我的IV是这样的:

92bff433cc639a6d
Run Code Online (Sandbox Code Playgroud)

Hai*_*vgi 55

使用功能: 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";
    }
}
?> 
Run Code Online (Sandbox Code Playgroud)

上面的例子将输出:

  • 该字符串AB10BC99由所有十六进制数字组成.
  • 该字符串AR1012不包含所有十六进制数字.
  • 该字符串ab12bc99由所有十六进制数字组成.


ngg*_*git 5

没有ctype或的另一种方式regex

$str = 'string to check';

if (trim($str, '0..9A..Fa..f') == '') {
    // string is hexadecimal
}
Run Code Online (Sandbox Code Playgroud)