Test whether string is valid integer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul E Collins

    Test whether string is valid integer

    Given a string variable (form input), how can I determine whether it
    represents a valid integer?

    is_numeric is true for floats as well as integers, and is_int always
    fails on a string.

    P.


  • Rutger Claes

    #2
    Re: Test whether string is valid integer

    Paul E Collins wrote:
    [color=blue]
    > Given a string variable (form input), how can I determine whether it
    > represents a valid integer?
    >
    > is_numeric is true for floats as well as integers, and is_int always
    > fails on a string.
    >
    > P.[/color]

    intval( $string ) == $string

    works for me...
    --
    Rutger Claes rgc@rgc.tld
    Replace tld with top level domain of belgium to contact me pgp:0x3B7D6BD6
    From listening comes wisdom and from speaking repentance.

    Comment

    • Paul E Collins

      #3
      Re: Test whether string is valid integer

      "Paul E Collins" <find_my_real_a ddress@CL4.org> wrote:
      [color=blue]
      > Given a string variable (form input), how can I
      > determine whether it represents a valid integer?[/color]

      Okay, I've come up with a solution:

      function isInteger($x)
      {
      return ($x === (string) (int) $x);
      }

      It returns false if $x has leading zeros, but that doesn't matter for
      my purposes.

      P.


      Comment

      • Justin Koivisto

        #4
        Re: Test whether string is valid integer

        Rutger Claes wrote:
        [color=blue]
        > Paul E Collins wrote:
        >
        >[color=green]
        >>Given a string variable (form input), how can I determine whether it
        >>represents a valid integer?
        >>
        >>is_numeric is true for floats as well as integers, and is_int always
        >>fails on a string.
        >>[/color]
        >
        > intval( $string ) == $string
        >
        > works for me...[/color]

        Depends on how strict you need to be...

        intval($n)==$n will return TRUE if you have a float like 5.0

        However, if you use:

        intval($n)===$n

        Then the string '5' would fail...

        If you need to reject floats, but need to use strings as well, you'll
        really need to do something like this (pulled from something I did a
        couple years ago):

        /**
        * input_intval
        *
        * Takes a variable and returns the integer value that corresponds
        * to the value. If the value cannot be represented by an integer,
        * FALSE is returned.
        *
        * @param $n [mixed] Value to check.
        * @result Returns an integer value or FALSE. Check for FALSE using
        * if(input_intval ($n)===FALSE) in case of integer < 0 being
        * returned.
        **/
        function input_intval($n ){
        if(is_numeric($ n)){
        if(is_int($n)){
        return $n;
        }else{
        // this is a float...
        if(intval($n)== $n){
        return $n;
        }else{
        return FALSE;
        }
        }
        }else{
        return FALSE;
        }
        }

        --
        Justin Koivisto - spam@koivi.com

        Comment

        • Justin Koivisto

          #5
          Re: Test whether string is valid integer

          Justin Koivisto wrote:
          [color=blue]
          > Rutger Claes wrote:
          >[color=green]
          >> Paul E Collins wrote:
          >>
          >>[color=darkred]
          >>> Given a string variable (form input), how can I determine whether it
          >>> represents a valid integer?
          >>>
          >>> is_numeric is true for floats as well as integers, and is_int always
          >>> fails on a string.
          >>>[/color]
          >>
          >> intval( $string ) == $string
          >>
          >> works for me...[/color]
          >
          >
          > Depends on how strict you need to be...
          >
          > intval($n)==$n will return TRUE if you have a float like 5.0
          >
          > However, if you use:
          >
          > intval($n)===$n
          >
          > Then the string '5' would fail...
          >
          > If you need to reject floats, but need to use strings as well, you'll
          > really need to do something like this (pulled from something I did a
          > couple years ago):
          >
          > /**
          > * input_intval
          > *
          > * Takes a variable and returns the integer value that corresponds
          > * to the value. If the value cannot be represented by an integer,
          > * FALSE is returned.
          > *
          > * @param $n [mixed] Value to check.
          > * @result Returns an integer value or FALSE. Check for FALSE using
          > * if(input_intval ($n)===FALSE) in case of integer < 0 being
          > * returned.
          > **/
          > function input_intval($n ){
          > if(is_numeric($ n)){
          > if(is_int($n)){
          > return $n;
          > }else{
          > // this is a float...
          > if(intval($n)== $n){
          > return $n;[/color]

          Sorry, that should have been:

          return intval($n);

          I was getting too far ahead of myself typing it out...
          [color=blue]
          > }else{
          > return FALSE;
          > }
          > }
          > }else{
          > return FALSE;
          > }
          > }
          >[/color]


          --
          Justin Koivisto - spam@koivi.com

          Comment

          • Tony Marston

            #6
            Re: Test whether string is valid integer

            Here is the code I use:

            $integer = (int)$value;
            if ((string)$value <> (string)$intege r) {
            $this->errors[$field] = "Value is not an integer";
            return $value;
            } // if

            --
            Tony Marston

            This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL



            "Paul E Collins" <find_my_real_a ddress@CL4.org> wrote in message
            news:cn0710$57d $1@titan.btinte rnet.com...[color=blue]
            > Given a string variable (form input), how can I determine whether it
            > represents a valid integer?
            >
            > is_numeric is true for floats as well as integers, and is_int always fails
            > on a string.
            >
            > P.
            >
            >[/color]


            Comment

            • Ilija Studen

              #7
              Re: Test whether string is valid integer

              Paul E Collins wrote:[color=blue]
              > Given a string variable (form input), how can I determine whether it
              > represents a valid integer?
              >
              > is_numeric is true for floats as well as integers, and is_int always
              > fails on a string.
              >
              > P.[/color]

              "Zero" sensitive function :) 5.0000 is integer 5.0001 is not...

              <?php

              function checkInteger($i nput) {

              // Check type
              if(is_integer($ input)) {
              return true;
              } // if

              // Typecast
              $in = (integer) $input;

              // Check strlen
              if(($in == $input) && (strlen($in) == strlen($input)) ) {
              return true;

              // No '.'?
              } elseif(strpos($ input, '.') === false) {
              return false;

              // We have '.'
              } else {

              // One for dot, other for zeros
              $dig_num = strlen($input) - strlen($in) - 1;

              // Get zeros
              $zeros = '.';
              for($i = 0; $i < $dig_num; $i++) {
              $zeros .= '0';
              } // form

              // Final check
              return (string) $in . $zeros == $input ? true : false;

              } // if

              } // func checkInteger

              // Test it with this code!
              print checkInteger('5 .00000') ? 'OK, int' : 'Not OK';

              ?>

              Comment

              Working...