开发者

Fix deprecated eregi function in php?

开发者 https://www.devze.com 2023-03-27 12:23 出处:网络
I\'m currently using a function to figure out if a 开发者_StackOverflow中文版string consists only of numbers. The function is_numeric usually works, but if the string contains a letter, it will break.

I'm currently using a function to figure out if a 开发者_StackOverflow中文版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


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

just add delimiters


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

case-insensitive i as modifier at the end


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号