PHP   发布时间:2022-04-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php – isHex()和isOcta()函数大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个功能. IsOcta和isHex.似乎无法使isHex正常工作.
isHex()中的问题是它不能省略原始字符串x23的’x’表示法.

原始十六进制srTing也可以是D1CE.所以添加x然后比较不会.

是否有正确的isHex函数解决方案. isOcta也正确吗?

function isHex($String){
    (int) $x=hexdec("$String");     // Input must be a String and hexdec returns numbER
    $y=dechex($X);          // Must be a number and dechex returns StriNG
    echo "<br />isHex() - Hexa number Reconverted: ".$y;       

    if($String==$y){
        echo "<br /> Result: Hexa ";
    }else{
        echo "<br /> Result: NOT Hexa";
    }   
    }


function IsOcta($String){
    (int) $x=octdec("$String");     // Input must be a String and octdec returns numbER
          $y=decoct($X);            // Must be a number and decoct returns StriNG
    echo "<br />IsOcta() - Octal number Reconverted: ".$y;          

    if($String==$y){
    echo "<br /> Result: OCTAL";
    }else{
    echo "<br /> Result: NOT OCTAL";
    }

}

这是对函数的调用:

$hex    =   "x23";      // StriNG 
$octa   =   "023";  // StriNG 
echo "<br /    Original HEX = $hex | Original Octa = $octa <br /    ";

echo isHex($heX)."<br /    ";   
echo IsOcta($octa);

这是函数调用的结果:

Original HEX = x23 | Original Octa = 023 

isHex() - Hexa number Reconverted: 23
Result: NOT Hexa

IsOcta() - Octal number Reconverted: 23
Result: OCTAL

=====完整的答案====

感谢Layke指导内置函数,该函数测试HEXA decimaL字符是否存在于字符串中.还要感谢马里奥提供使用ltrim的提示.这两个函数都需要获得isHexa或者是要构建的十六进制函数.

—编辑功能 –

// isHEX function

function isHex($Strings){

// Does not work as originally suggested by Layke,but thanks for direcTing to the resource. It does not omit 0x representation of a hexadecimal number. 
/*
foreach ($Strings as $TESTCasE) {
     if (ctype_xdigit($TESTCasE)) {
        echo "<br /> $TESTCase - <strong>TRUE</strong>,Contains only Hex<br />";
    } else {
        echo "<br /> $TESTCase - false,Is not Hex";

   } 
}
*/

   // This works CORRECTLY
   foreach ($Strings as $TESTCasE) {
        if (ctype_xdigit(ltrim($TESTCase,"0x"))) {
           echo "<br /> $TESTCase - <strong>TRUE</strong>,Contains only Hex<br />";
       } else {
           echo "<br /> $TESTCase - false,Is not Hex";

      } 
   }
}

$Strings = array('AB10BC99','AR1012','x23','0x12345678');
isHex($Strings); // calling

可能现在,这是’十六进制’功能的傻瓜证明答案吗?

解决方法

isHexadecimal?

php内置了十六进制函数.

请参阅此处的函数ctype_xdigit:http://uk1.php.net/ctype_xdigit

<?php
$Strings = array('AB10BC99','ab12bc99');
foreach ($Strings as $TESTCasE) {
    if (ctype_xdigit($TESTCasE)) {
        // TRUE : Contains only Hex
    } else {
        // false : Is not Hex
    }
}

isOctal?

并且为了确定数字是否为octal,您可以翻转和翻转.

function isOctal($X) {
    return decoct(octdec($X)) == $x;
}

大佬总结

以上是大佬教程为你收集整理的php – isHex()和isOcta()函数全部内容,希望文章能够帮你解决php – isHex()和isOcta()函数所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:php函数