0

I'm currently using a function to figure out if a string consists only of numbers. The function is_numeric usually works, but if the string contains a letter, it will break. I am trying this instead:

function is_signedint($val)
{
    $val = str_replace(" ", "", trim($val));
    //line below is deprecated
    $bool = eregi("^-?([0-9])+$",$val);
    if($bool == 1)
        return true;
    else
        return false;
}

Anyways, I was wondering how I could replace the eregi line to comply with PHP6

hakre
  • 193,403
  • 52
  • 435
  • 836
hetoan2
  • 226
  • 4
  • 14
  • possible duplicate of [Converting ereg expressions to preg](http://stackoverflow.com/questions/6270004/converting-ereg-expressions-to-preg) – mario Aug 09 '11 at 23:24

3 Answers3

2
preg_match("~^-?([0-9])+$~", $val);

just add delimiters

genesis
  • 50,477
  • 20
  • 96
  • 125
0
preg_match("/^-?([0-9])+$/i",$val);

case-insensitive i as modifier at the end

Jacek Kaniuk
  • 5,229
  • 26
  • 28
0

Here's a couple of options

if ($val === strval(intval($val)))

if ($val == intval($val))

I would use the first method as it checks the value and type. You shouldn't use regular expressions unless it is really necessary (or it can squash 10+ lines of code into 1).

adlawson
  • 6,303
  • 1
  • 35
  • 46