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).
精彩评论