PHP判断FORM表单或URL参数来的数据是否为整数的方法_php技巧

WBOY
Release: 2016-05-16 19:55:43
Original
1469 people have browsed it

PHP判断FORM表单或URL参数来的数据是否为整数,is_int函数对于FORM表单或URL参数过来的数据是没有办法判断是否是整数的,因为FORM过来的是字符串。
用is_numeric可以判断是否为数字类型,再判断是否有小数点就可以判断是不是整数了

if(!is_numeric($page)||strpos($page,".")!==false){
echo "不是整数";
}else{
echo "是整数";
}
Copy after login

有时候我们需要判断id是否为数字的方法:

dedecms中的判断数字的方法

$cid = empty($cid)? 1 : intval(preg_replace("/[^-\d]+[^\d]/",'', $cid));
Copy after login

建议大家对关键的参数必须做过滤。如数字正则过滤

复制代码 代码如下:

if(preg_match("/^\d*$/",$fgid))    echo('是数字');
else   echo('不是数字');

或者用函数
复制代码 代码如下:

if(is_numeric($fgid)) echo('是数字');
else echo('不是数字');
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!