SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Evangelist hessodreamy's Avatar
    Join Date
    Apr 2005
    Location
    uk
    Posts
    525
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    php function is_integer_or_string_integer ?

    It makes me feel like a right noob asking this, but is there any built-in php function that will tell you whether a variable represents an integer, or a string integer eg 2 or "2" but not 2.4 or "2.4".

    is_int() will be false if it is a string.
    is_numeric will allow decimal places and +- signs.

    I usually just use my own function, but am I missing something built in?

  2. #2
    SitePoint Zealot
    Join Date
    Jan 2011
    Location
    Portland
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    is_int(str_replace(array('"' "'"), '' $your_var)) 
    or

    PHP Code:
    function isInt($int){
      return 
    is_int(str_replace(array('"' "'"), '' ,  $int));

    coming soon sitejuju.com my new development portfolio

  3. #3
    SitePoint Evangelist hessodreamy's Avatar
    Join Date
    Apr 2005
    Location
    uk
    Posts
    525
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jgetner View Post
    PHP Code:
    function isInt($int){
      return 
    is_int(str_replace(array('"' "'"), '' ,  $int));

    That doesn't work because is_int is evaluating a string, and returning false.

    PHP Code:
    $a "2";
    function 
    isInt($int){ 
      return 
    is_int(str_replace(array('"' "'"), '' ,  $int)); 


    var_dump(isInt($a)); 
    Outputs
    Code:
    bool(false)

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2011
    Location
    Portland
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm ok i had to go re-write this whole thing because that was annoying me. This code is tested and works.

    PHP Code:
    function isInt($int){
        
    // boolean true or false if $int is 0
        
    $zero false;
        
        
    // compares the argument to quoted 0
        
    if(str_replace(array("'" '"') , '"' $int) == "0"){
            
    $zero true;    
        }
        
        
    // convert the argument to an int no matter what.
         
    $int = (int) $int;
        
        
    // is the value a zero and the argument is a 0
        
    if($int == and $zero == true){
            return 
    true;
        }
        
        
    // is the value a zero and the argument not a 0
        
    elseif($int == and $zero == false){
            return 
    false;
        }
        
        
    // is it not a 0 and an int
        
    elseif($int !== and is_int($int)){
            return 
    true;
        }
        
        
    // false for all else
        
    return false;

    coming soon sitejuju.com my new development portfolio

  5. #5
    SitePoint Zealot
    Join Date
    Feb 2012
    Location
    United Kingdom
    Posts
    114
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    This function should achieve what you're trying to do:
    PHP Code:
    function isInt($arg)
    {
        return (
    $arg == '0' || filter_var($argFILTER_VALIDATE_INT)) ? TRUE FALSE;

    It will return TRUE to the following:
    Code:
    0
    '0'
    5
    '5'
    And FALSE to:
    Code:
    1.1
    '1.1'
    'w'

  6. #6
    SitePoint Zealot
    Join Date
    Jan 2011
    Location
    Portland
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice ModernW, I did not think of using filter_var.
    coming soon sitejuju.com my new development portfolio

  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    or:
    PHP Code:
    if ( is_int$var ) OR ctype_digit$var ) )
      
    // ... 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  8. #8
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    hessodreamy, could you clarify exactly what values you want to allow? You briefly mentioned signs, in relation to is_numeric(); do you want to return false if any sign is encountered, or only for negative? What range of values do you want to accept? The range of the integer type varies between signed/unsigned and 32/64-bit machines. Since you're accepting strings, will your integers be bound by any of these or accept an arbitrarily long string integer? Leading zeros, are they allowed?

    I ask the above because the existing code offered will return true for "is int" in cases where you, possibly, may not want them to do so. For example, FILTER_VALIDATE_INT will allow a sign and is bound by the integer range of PHP, and ctype_digit() allows leading zeros but accepts any length of string.
    Salathe
    Software Developer and PHP Manual Author.

  9. #9
    SitePoint Evangelist hessodreamy's Avatar
    Join Date
    Apr 2005
    Location
    uk
    Posts
    525
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Well I do have a solution using preg_match:
    PHP Code:
    function isInt($input)
    {
           return 
    preg_match("/^[0-9]+$/",trim($input));

    (or something like that - I'm not at my normal PC)

    And this works fine for what I want (although other solutions may be slightly faster). Ie to allow only whole, simple, positive numbers, whether it be an integer literal or a string literal. No decimals. no signs. No other string characters. Basically what you'd expect out of a database auto increment primary key.

    I was just wondering if I had been missing some native PHP function to do this, but I guess there isn't one because I'm actually wanting to exclude some valid integer elements like signs.

  10. #10
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    ctype_digit() will do what your regex does.
    Salathe
    Software Developer and PHP Manual Author.

  11. #11
    SitePoint Evangelist hessodreamy's Avatar
    Join Date
    Apr 2005
    Location
    uk
    Posts
    525
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    ctype_digit() will do what your regex does.
    Except it will return false if you give it an actual integer, won't it (at least that's what it says in the manual)?

  12. #12
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hessodreamy View Post
    Except it will return false if you give it an actual integer, won't it (at least that's what it says in the manual)?
    http://www.sitepoint.com/forums/show...=1#post5188948
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  13. #13
    SitePoint Evangelist hessodreamy's Avatar
    Join Date
    Apr 2005
    Location
    uk
    Posts
    525
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Yeah, that's a very elegant solution. I think we've established that here isn't a native function that'll do exactly what I want.

  14. #14
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hessodreamy View Post
    Except it will return false if you give it an actual integer, won't it (at least that's what it says in the manual)?
    Depends on the integer (read the manual for why). Also, that's the reason why I said "your regex" instead of "your function".
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •