hetoan2
hetoan2

Reputation: 226

Fix deprecated eregi function in php?

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

Upvotes: 0

Views: 357

Answers (3)

adlawson
adlawson

Reputation: 6431

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).

Upvotes: 0

genesis
genesis

Reputation: 50976

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

just add delimiters

Upvotes: 2

Jacek Kaniuk
Jacek Kaniuk

Reputation: 5229

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

case-insensitive i as modifier at the end

Upvotes: 0

Related Questions