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
  • 184,866
  • 48
  • 414
  • 792
hetoan2
  • 226
  • 3
  • 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
  • 49,367
  • 20
  • 94
  • 122
0
preg_match("/^-?([0-9])+$/i",$val);

case-insensitive i as modifier at the end

Jacek Kaniuk
  • 5,153
  • 24
  • 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,155
  • 1
  • 33
  • 46