Checking if a string contains an integer

后端 未结 14 662
北海茫月
北海茫月 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:38

    There may be two cases-

    1. You need to check for exact string format of a number(most of ans is about this one)

    2. You want to check, whether a string contains a specific number or not

      preg_match('/'.$matching_number.'/',$container_string);

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

    Other option

    function holds_int($str)
       {
       return preg_match("/^-?[0-9]+$/", $str);
       }
    
    0 讨论(0)
  • 2020-12-02 17:39

    comparison.php:

    <?php
    function is_numeric_int($s)
    {
      return (strval(intval($s)) === $s);
    }
    
    function print_ini($s)
    {
      echo "$s: " . ( is_numeric_int($s) ? 'true' : 'false' ) . "\n";
    }
    
    print_ini('qwe');
    print_ini('23');
    print_ini('-23');
    print_ini('23.0');
    print_ini('-23.0');
    ?>
    

    Test run:

    $ php comparison.php 
    qwe: false
    23: true
    -23: true
    23.0: false
    -23.0: false
    
    0 讨论(0)
  • 2020-12-02 17:40

    I've been using this since long time. While all the other answers have drawbacks or special cases, if you want to detect any possible int valued thing, including 1.0 "1.0" 1e3 "007" then you better let is_numeric do its job to detect any possible PHP object that represents a number, and only then check if that number is really an int, converting it to int and back to float, and testing if it changed value.:

    function isIntValued($var) {
        if(is_numeric($var)) { // At least it's number, can be converted to float
            $var=(float)$var; // Now it is a float
            return ((float)(int)$var)===$var;
        }
        return FALSE;
    }
    

    or in short

    function isIntValued($var) {
        return (!is_numeric($var)?FALSE:((float)(int)(float)$var)===(float)$var);
    }
    

    Or

     function isIntValued($var) {
        return (is_numeric($var) && ((float)(int)(float)$var)===(float)$var);
    }
    

    Note that while PHP's is_int() checks if the type of variable is an integer, on the contrary the other standard PHP function is_numeric() determines very accurately if the contents of the variable (i.e. string chars) can represent a number.

    If, instead, you want "1.0" and "2.00" not to be considered integers but floats (even if they have an integer value), then the other answer ( @Darragh Enright ) using is_numeric, adding zero and testing for int is probably the most correct solution:

        is_numeric($s) && is_int($s+0)
    
    0 讨论(0)
  • 2020-12-02 17:45

    Update Since PHP 7.1 there are problems with using is_int() with non-numeric values, as discussed in this SO Answer. In any case, this is a very old answer and I'd really view it as something of a hack at this point so YMMV ;)

    Sorry if this question has been answered but this has worked for me in the past:

    First check if the string is_numeric. if it is add a 0 to the value to get PHP to covert the string to its relevant type. Then you can check if it's an int with is_int. Quick and dirty but it works for me...

    $values = array(1, '2', '2.5', 'foo', '0xFF', 0xCC, 0644, '0777');
    
    foreach ($values as $value) {
      $result = is_numeric($value) && is_int(($value + 0)) ? 'true' : 'false';
      echo $value . ': ' . $result . '<br />';
    }
    

    Results:

    1: true
    2: true
    2.5: false
    foo: false
    0xFF: true
    204: true
    420: true
    0777: true
    

    The only problem is that it will evaluate octal values wrapped in a string literally, i.e: '0123' will simply become 123. But that's easy to address :)

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

    I liked nyson's suggestion, but noticed that it will be false for '0123'. I'm now doing this:

    (string)(int)$var === ltrim((string)$var, '0')
    

    (This would have been posted as a comment @nyson, but I don't have enough privileges to do that yet).

    Edited to add: If you want zero to be true, you need to do something like

    (int)$var === 0 || (string)(int)$var === ltrim((string)$var, '0')
    
    0 讨论(0)
提交回复
热议问题