I am trying to test a variable to determine whether a number (as opposed to an alpha or other character) is present. I use the following code to test for single alpha chars:

if (("a" <= $ModSuffix && $ModSuffix <= "z") || ("A" <= $ModSuffix && $ModSuffix <= "Z"))

This seems to work well in isolating alpha chars. I tried the following to isolate numbers in the same variable:

if (($ModSuffix > 0) || ($ModSuffix < 100000)) { //must be a number

Unfortunately, this test allows non-numeric chars through (such as ~,,(,),|, etc.) I'm guessing there is some simple PHP command that I'm just missing, but I defer to the greater knowledge of the overall collective. Any ideas?

Thanks,
Lenny

    using an ereg is probably the easier way:

    $string = "234";
    
    if(ereg('^[0-9]*$', $string)) {
        // it's a number
    }
    

    ^ = must denote beginning
    $ = must denote end
    [0-9] = must be a digit from 0 through 9
    * = any number of the pattern may occur... i.e. the number can be however long.

    -sridhar

      Excellent response. It works very well and solves the problem. While I've used ereg before, I don't know why I didn't think of it this time. Thanks for the effective and concise response.
      Lenny

        With preg_match the regexp is shorter - "/\d+$/" - but is_numeric($string) is probably quite sufficient.

          Why on earth not just check is_numeric()? Regexp's are neat and stuff, but really...

            Write a Reply...