2

How do I write a function that returns FALSE if a given string is NOT A VALID NUMBER OF TYPE [PHP INT], and returns TRUE otherwise.

This is simple in other languages.

intval(), isint(), and is_numeric() are not adequate, here is why:

is_numeric() is not adequate because it matches to any number not just integers, also it accepts huge numbers as numeric, which aren't integers. intval() is not adequate because it returns 0 for BOTH invalid PHP integers like '9000000000000000' AND the valid PHP integer '0' or '0x0' or '0000' etc. isint() only tests if a variable is already of type int, it doesn't deal with strings or conversion to int.

Maybe there is a popular library for this or something?

I want to call a function that is capable of detecting whether the form data someone posts is a valid php integer, for instance.

I want to call the function that does this: is_php_integer($str_test_input). What goes in the function?

<?php

$strInput = 'test' //function should return FALSE
$strInput = '' //function should return FALSE
$strInput = '9000000000000000'  //function should return FALSE since
                            //is not valid int in php
$strInput = '9000' //function should return TRUE since
                    //valid integer in php
$strInput = '-9000' // function should return TRUE
$strInput = '0x1A' // function should return TRUE
                    // since 0x1A = 26, a valid integer in php
$strInput = '0' // function should return TRUE, since
                    // 0 is a valid integer in php
$strInput = '0x0' // function should return TRUE, since
                    // 0x0 = 0 which is a valid integer in php
$strInput = '0000' // function should return TRUE, since
                    // 0000 = 0 which is a valid integer in php

function is_php_integer($strTestInput) {
    // what goes here?
    // ...
    // if string could be interpreted as php integer, return true
    // else, return false
}

if is_php_integer($strInput) {
    echo 'your integer plus one equals: '. (intval($strInput) + 1);
} else {
    echo 'your input string is not a valid php integer'
}

?>

Thanks in advance!

4

3 回答 3

3
<?php

$input = array(
    'test',
    '',
    '9000000000000000',
    '9000',
    '-9000',
    '0x1A',
    '0',
    '0x0',
    '0000'
);

function is_php_integer($strTestInput) {
    return filter_var( $strTestInput, FILTER_VALIDATE_INT, array('flags' => FILTER_FLAG_ALLOW_OCTAL | FILTER_FLAG_ALLOW_HEX));

}

foreach ( $input as $value ) {
    if (is_php_integer($value) !== FALSE) {
        echo 'your integer plus one equals: '. (intval( $value ) + 1) . PHP_EOL;
    } else {
        echo 'your input string is not a valid php integer' . PHP_EOL;
    }
}
于 2013-01-21T22:22:54.950 回答
2

您可以使用filter_varFILTER_FLAG_ALLOW_HEX标志对于0x1Aand是必要的0x0FILTER_FLAG_ALLOW_OCTAL对于 也是必要的0000

function is_php_integer($strInput) {
    return filter_var(
        $strInput, FILTER_VALIDATE_INT,
        FILTER_FLAG_ALLOW_OCTAL | FILTER_FLAG_ALLOW_HEX
    ) !== false;
}
于 2013-01-21T22:30:47.457 回答
1
function is_php_integer($strInput) {
    return false !== filter_var($strInput, FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_HEX);
}

另外,考虑filter_input直接过滤表单数据。

于 2013-01-21T22:24:27.583 回答