Checking if a string contains an integer

后端 未结 14 660
北海茫月
北海茫月 2020-12-02 17:10

Do you know of a function that can check if a string contains an integer?

Here\'s how I\'d expect it to work:

holds_int(\"23\") // should return true         


        
相关标签:
14条回答
  • 2020-12-02 17:28

    is_int is the only what it's meant to do this work.

    0 讨论(0)
  • 2020-12-02 17:29

    Not the fastest method, but filter_var() is quite accurate:

    function test($s)
    {
        return filter_var($s, FILTER_VALIDATE_INT) !== false;
    }
    

    Here are the results based on Jhong's answer, differences marked with !!:

    var_dump(test(1)            ); // true
    var_dump(test('1')          ); // true
    var_dump(test('1.0')        ); // false !!
    var_dump(test('1.1')        ); // false
    var_dump(test('0xFF')       ); // false
    var_dump(test('0123')       ); // false !!
    var_dump(test('01090')      ); // false !!
    var_dump(test('-1000000')   ); // true
    var_dump(test('+1000000')   ); // true
    var_dump(test('2147483648') ); // true !! on 64bit
    var_dump(test('-2147483649')); // true !! on 64bit
    

    To allow octal integers:

    function test($s)
    {
       return filter_var($s, FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_OCTAL) !== false;
    }
    

    Results:

    var_dump(test('0123') ); // true
    var_dump(test('01090')); // false !!
    

    To allow hexadecimal notation:

    function test($s)
    {
       return filter_var($s, FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_HEX) !== false;
    }
    

    Results:

    var_dump(test('0xFF')); // true !!
    
    0 讨论(0)
  • 2020-12-02 17:29

    Maybe this will also help in given situation there is a function in php that already does this, its called "is_numeric()" it will return true or false accordenly..

       if(is_numeric($somestring) == True){
            echo "this string contains a integar";
        }
    

    link: http://www.php.net/is_numeric

    you said "holdsint("2") should return true, well is_numeric("2") returns True, and is_numeric("a") False, as expected, this function exists in php, no need to rewrite.

    0 讨论(0)
  • 2020-12-02 17:30
    if((string)(int)$var == $var) {
        echo 'var is an integer or a string representation of an integer';
    }
    

    Example results:

    var_dump( test(1)             ); // TRUE
    var_dump( test('1')           ); // TRUE
    var_dump( test('1.0')         ); // TRUE
    var_dump( test('1.1')         ); // false
    var_dump( test('0xFF')        ); // false
    var_dump( test('0123')        ); // TRUE
    var_dump( test('01090')       ); // TRUE
    var_dump( test('-1000000')    ); // TRUE
    var_dump( test('+1000000')    ); // TRUE
    var_dump( test('2147483648')  ); // false
    var_dump( test('-2147483649') ); // false
    

    See Gordon's answer below for how this would behave differently if === were used for comparison instead of ==.

    0 讨论(0)
  • 2020-12-02 17:35

    Dont want to accidently turn Jhong's answer into a CW, so for the record here is the results when testing with === instead of ==.

    function test($var) {
        return ((string)(int)$var === $var);
    }
    
    var_dump( test(1)             ); // returns false vs TRUE
    var_dump( test('1')           ); // returns TRUE
    var_dump( test('1.0')         ); // returns false vs TRUE
    var_dump( test('1.1')         ); // returns false 
    var_dump( test('0xFF')        ); // returns false
    var_dump( test('0123')        ); // returns false vs TRUE
    var_dump( test('-0123')       ); // returns false vs TRUE
    var_dump( test('-1000000')    ); // returns TRUE
    var_dump( test('+1000000')    ); // returns false vs TRUE
    var_dump( test('2147483648')  ); // returns false
    var_dump( test('-2147483649') ); // returns false
    
    0 讨论(0)
  • 2020-12-02 17:38

    ctype_digit will do the trick:

    ctype_digit($str)

    1. $str has to be a string
    2. extra spaces are not allowed
    3. no decimal points .
    0 讨论(0)
提交回复
热议问题