PHP_判断是否为数字

前两天写PHP代码的时候遇到要判断一个变量是否为数字,所以就把相关的文章简单的摘抄了过来:

php 正则验证字符串是否为数字
方法一:
php中利用正则表达式验证字符串是否为数字一件非常容易的事情,最主要的是如何写好正则表达式以及掌握正则表达式的写法,在此利用正则表达式的方式来列举一下判断数字的方法。

<?

if($str)
{
  if(eregi("^[0-9]+$",$str))
  {
    $str=(int)$str;
  }
  else
  {
    echo "获取到的数据不是有效的数字类型,操作将停止!";
    exit();
  }
}
else
{
  echo "需要验证的数据为空,操作停止!";
  exit();
}

?>

方法二:
建议大家对关键的参数必须做过滤。如数字正则过滤
if(preg_match("/^\d*$/",$fgid))<wbr><wbr><wbr>echo('是数字');<br> else<wbr><wbr>echo('不是数字');<br> 或者用函数<br> if(is_numeric($fgid)) echo('是数字');<br> else echo('不是数字');[/code]这两种方法的区别是<wbr>is_numeric小数也会认为是数字,而前面正则会把小数点当作字符。</wbr></wbr></wbr></wbr></wbr></wbr>

附一些常用的正则运算:

验证数字:^[0-9]*$
验证n位的数字:^\d{n}$
验证至少n位数字:^\d{n,}$
验证m-n位的数字:^\d{m,n}$
验证零和非零开头的数字:^(0|[1-9][0-9]*)$
验证有两位小数的正实数:^[0-9]+(.[0-9]{2})?$
验证有1-3位小数的正实数:^[0-9]+(.[0-9]{1,3})?$
验证非零的正整数:^\+?[1-9][0-9]*$
验证非零的负整数:^\-[1-9][0-9]*$
验证非负整数(正整数 + 0)<wbr style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">^\d+$</span><br style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">验证非正整数(负整数 + 0)</span><wbr style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">^((-\d+)|(0+))$</span><br style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">验证长度为3的字符:^.{3}$</span><br style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">验证由26个英文字母组成的字符串:^[A-Za-z]+$</span><br style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">验证由26个大写英文字母组成的字符串:^[A-Z]+$</span><br style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">验证由26个小写英文字母组成的字符串:^[a-z]+$</span><br style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">验证由数字和26个英文字母组成的字符串:^[A-Za-z0-9]+$</span><br style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">验证由数字、26个英文字母或者下划线组成的字符串:^\w+$</span><br style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">验证用户密码:^[a-zA-Z]\w{5,17}$ 正确格式为:以字母开头,长度在6-18之间,只能包含字符、数字和下划线。</span><br style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">验证是否含有 ^%&amp;‘,;=?$\” 等字符:[^%&amp;‘,;=?$\x22]+</span><br style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">验证汉字:^[\u4e00-\u9fa5],{0,}$</span><br style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">验证Email地址:^\w+[-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$</span><br style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">验证InternetURL:^http://([\w-]+\.)+[\w-]+(/[\w-./?%&amp;=]*)?$ ;^[a-zA-z]+://(w+(-w+)*)(.(w+(-w+)*))*(?S*)?$</span><br style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">验证电话号码:^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$:–正确格式为:XXXX-XXXXXXX,XXXX-XXXXXXXX,XXX-XXXXXXX,XXX-XXXXXXXX,XXXXXXX,XXXXXXXX。</span><br style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">验证身份证号(15位或18位数字):^\d{15}|\d{}18$</span><br style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">验证一年的12个月:^(0?[1-9]|1[0-2])$ 正确格式为:“01”-“09”和“1”“12”</span><br style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">验证一个月的31天:^((0?[1-9])|((1|2)[0-9])|30|31)$</span><wbr style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">正确格式为:01、09和1、31。</span><br style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">整数:^-?\d+$</span><br style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">非负浮点数(正浮点数 + 0):^\d+(\.\d+)?$</span><br style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">正浮点数</span><wbr style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$</span><br style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">非正浮点数(负浮点数 + 0) ^((-\d+(\.\d+)?)|(0+(\.0+)?))$</span><br style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">负浮点数</span><wbr style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$</span><br style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">浮点数</span><wbr style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(50,62,50); font-family:simsun; font-size:14px; line-height:21px">^(-?\d+)(\.\d+)?</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

你可能感兴趣的:(PHP)