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

时间:2021-04-27 15:45:34

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:

我不知道如何验证这个字符串。我只是提供了一个用于加密的IV,但是找不到1is_hex()1或类似的函数,我无法完全理解它!我阅读了php文档中的一个注释(用户懊悔)。指出):

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

7 个解决方案

#1


49  

Use function : ctype_xdigit

使用功能: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.
  • 字符串AB10BC99由所有十六进制数字组成。
  • The string AR1012 does not consist of all hexadecimal digits.
  • 字符串AR1012不包含所有十六进制数字。
  • The string ab12bc99 consists of all hexadecimal digits.
  • 字符串ab12bc99由所有十六进制数字组成。

#2


3  

Is there any reason not to match against a simple RE like "[0-9A-Fa-f]+"? (edit: possibly with a '^' at the beginning and '$' at the end to assure you've matched the whole string).

有什么理由不跟一个简单的“[0-9A-Fa-f]+”匹配吗?(编辑:可能开始时“^”和“$”结束时向你保证匹配整个字符串)。

#3


1  

The perfect way to check HEX string works from PHP 4 and above.

检查十六进制字符串的最佳方法来自PHP 4及以上版本。

<?php
function is_hex($hex_code) {
        return @preg_match("/^[a-f0-9]{2,}$/i", $hex_code) && !(strlen($hex_code) & 1);
}
?>

#4


1  

Your input is too large. From the PHP manual of dexhex

你的投入太大了。来自dexhex的PHP手册

The largest number that can be converted is 4294967295 in decimal resulting to "ffffffff"

可以转换的最大数字是4294967295,小数形式为“ffffffff”

So you'll be better off using a RegEx, which have already been supplied here by others.

所以你最好使用RegEx,它已经由其他人提供了。

#5


1  

Another way without ctype or regex:

没有ctype或regex的另一种方式:

$str = 'string to check';

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

#6


0  

This is also possible and quite simple

这也是可能的,而且非常简单

$a="affe";  //is_hex
$b="a0bg";   //is_not_hex

if(is_numeric('0x'.$a)) echo 'is_hex';
else echo 'is_not_hex';

if(is_numeric('0x'.$b)) echo 'is_hex';
else echo 'is_not_hex';

#7


0  

Add the case-insensitive 'i' flag

添加不区分大小写的“i”标记。

preg_match('/^[0-9a-f]+$/i', ...

#1


49  

Use function : ctype_xdigit

使用功能: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.
  • 字符串AB10BC99由所有十六进制数字组成。
  • The string AR1012 does not consist of all hexadecimal digits.
  • 字符串AR1012不包含所有十六进制数字。
  • The string ab12bc99 consists of all hexadecimal digits.
  • 字符串ab12bc99由所有十六进制数字组成。

#2


3  

Is there any reason not to match against a simple RE like "[0-9A-Fa-f]+"? (edit: possibly with a '^' at the beginning and '$' at the end to assure you've matched the whole string).

有什么理由不跟一个简单的“[0-9A-Fa-f]+”匹配吗?(编辑:可能开始时“^”和“$”结束时向你保证匹配整个字符串)。

#3


1  

The perfect way to check HEX string works from PHP 4 and above.

检查十六进制字符串的最佳方法来自PHP 4及以上版本。

<?php
function is_hex($hex_code) {
        return @preg_match("/^[a-f0-9]{2,}$/i", $hex_code) && !(strlen($hex_code) & 1);
}
?>

#4


1  

Your input is too large. From the PHP manual of dexhex

你的投入太大了。来自dexhex的PHP手册

The largest number that can be converted is 4294967295 in decimal resulting to "ffffffff"

可以转换的最大数字是4294967295,小数形式为“ffffffff”

So you'll be better off using a RegEx, which have already been supplied here by others.

所以你最好使用RegEx,它已经由其他人提供了。

#5


1  

Another way without ctype or regex:

没有ctype或regex的另一种方式:

$str = 'string to check';

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

#6


0  

This is also possible and quite simple

这也是可能的,而且非常简单

$a="affe";  //is_hex
$b="a0bg";   //is_not_hex

if(is_numeric('0x'.$a)) echo 'is_hex';
else echo 'is_not_hex';

if(is_numeric('0x'.$b)) echo 'is_hex';
else echo 'is_not_hex';

#7


0  

Add the case-insensitive 'i' flag

添加不区分大小写的“i”标记。

preg_match('/^[0-9a-f]+$/i', ...