<p> PHP动态网页开发中常用的表单验证类<code><?php<br />class class_post<br />{<br />//验证是否为指定长度的字母/数字组合<br />function fun_text1($num1,$num2,$str)<br />{<br />Return (preg_match("/^[a-zA-Z0-9]{".$num1.",".$num2."}$/",$str))?true:false;<br />}<br />//验证是否为指定长度数字<br />function fun_text2($num1,$num2,$str)<br />{<br />return (preg_match("/^[0-9]{".$num1.",".$num2."}$/i",$str))?true:false;<br />}<br />//验证是否为指定长度汉字<br />function fun_font($num1,$num2,$str)<br />{<br />// preg_match("/^[xa0-xff]{1,4}$/", $string);<br />return (preg_match("/^([x81-xfe][x40-xfe]){".$num1.",".$num2."}$/",$str))?true:false;<br />}<br />//验证身份证号码<br />function fun_status($str)<br />{<br />return (preg_match('/(^([d]{15}|[d]{18}|[d]{17}x)$)/',$str))?true:false;<br />}<br />//验证邮件地址<br />function fun_email($str){<br />return (preg_match('/^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,4}$/',$str))?true:false;<br />}<br />//验证电话号码<br />function fun_phone($str)<br />{<br />转自:网页教学网[www.webjx.com]<br />return (preg_match("/^(((d{3}))|(d{3}-))?((0d{2,3})|0d{2,3}-)?[1-9]d{6,7}$/",$str))?true:false;<br />}<br />//验证邮编<br />function fun_zip($str)<br />{<br />return (preg_match("/^[1-9]d{5}$/",$str))?true:false;<br />}<br />//验证url地址<br />function fun_url($str)<br />{<br />return (preg_match("/^http://[A-Za-z0-9]+.[A-Za-z0-9]+[/=?%-&_~`@[]':+!]*([^<>""])*$/",$str))?true:false;<br />}<br />// 数据入库 转义 特殊字符 传入值可为字符串 或 一维数组<br />function data_join(&$data)<br />{<br />if(get_magic_quotes_gpc() == false)<br />{<br />if (is_array($data))<br />{<br />foreach ($data as $k => $v)<br />{<br />$data[$k] = addslashes($v);<br />}<br />}<br />else<br />{<br />$data = addslashes($data);<br />}<br />}<br />Return $data; return (preg_match("/^(((d{3}))|(d{3}-))?((0d{2,3})|0d{2,3}-)?[1-9]d{6,7}$/",$str))?true:false;<br />}<br />//验证邮编<br />function fun_zip($str)<br />{<br />return (preg_match("/^[1-9]d{5}$/",$str))?true:false;<br />}<br />//验证url地址<br />function fun_url($str)<br />{<br />return (preg_match("/^http://[A-Za-z0-9]+.[A-Za-z0-9]+[/=?%-&_~`@[]':+!]*([^<>""])*$/",$str))?true:false;<br />}<br />// 数据入库 转义 特殊字符 传入值可为字符串 或 一维数组<br />function data_join(&$data)<br />{<br />if(get_magic_quotes_gpc() == false)<br />{<br />if (is_array($data))<br />{<br />foreach ($data as $k => $v)<br />{<br />$data[$k] = addslashes($v);<br />}<br />}<br />else<br />{<br />$data = addslashes($data);<br />}<br />}<br />Return $data;<br />}<br />转自:网页教学网[www.webjx.com]<br />// 数据出库 还原 特殊字符 传入值可为字符串 或 一/二维数组<br />function data_revert(&$data)<br />{<br />if (is_array($data))<br />{<br />foreach ($data as $k1 => $v1)<br />{<br />if (is_array($v1))<br />{<br />foreach ($v1 as $k2 => $v2)<br />{<br />$data[$k1][$k2] = stripslashes($v2);<br />}<br />}<br />else<br />{<br />$data[$k1] = stripslashes($v1);<br />}<br />}<br />}<br />else<br />{<br />$data = stripslashes($data);<br />}<br />Return $data;<br />}<br />转自:网页教学网[www.webjx.com]<br />// 数据显示 还原 数据格式 主要用于内容输出 传入值可为字符串 或 一/二维数组<br />// 执行此方法前应先data_revert(),表单内容无须此还原<br />function data_show(&$data)<br />{<br />if (is_array($data))<br />{<br />foreach ($data as $k1 => $v1)<br />{<br />if (is_array($v1))<br />{<br />foreach ($v1 as $k2 => $v2)<br />{<br />$data[$k1][$k2]=nl2br(htmlspecialchars($data[$k1][$k2]));<br />$data[$k1][$k2]=str_replace(" "," ",$data[$k1][$k2]);<br />$data[$k1][$k2]=str_replace(" ","<br> ",$data[$k1][$k2]);<br />}<br />}<br />else<br />{<br />$data[$k1]=nl2br(htmlspecialchars($data[$k1]));<br />$data[$k1]=str_replace(" "," ",$data[$k1]);<br />$data[$k1]=str_replace(" ","<br> ",$data[$k1]);<br />}<br />}<br />}<br />else<br />{<br />$data=nl2br(htmlspecialchars($data));<br />$data=str_replace(" "," ",$data);<br />$data=str_replace(" ","<br> ",$data);<br />}<br />Return $data;<br />}<br />}<br />?> if (is_array($data))<br />{<br />foreach ($data as $k1 => $v1)<br />{<br />if (is_array($v1))<br />{<br />foreach ($v1 as $k2 => $v2)<br />{<br />$data[$k1][$k2]=nl2br(htmlspecialchars($data[$k1][$k2]));<br />$data[$k1][$k2]=str_replace(" "," ",$data[$k1][$k2]);<br />$data[$k1][$k2]=str_replace(" ","<br> ",$data[$k1][$k2]);<br />}<br />}<br />else<br />{<br />$data[$k1]=nl2br(htmlspecialchars($data[$k1]));<br />$data[$k1]=str_replace(" "," ",$data[$k1]);<br />$data[$k1]=str_replace(" ","<br> ",$data[$k1]);<br />}<br />}<br />}<br />else<br />{<br />$data=nl2br(htmlspecialchars($data));<br />$data=str_replace(" "," ",$data);<br />$data=str_replace(" ","<br> ",$data);<br />}<br />Return $data;<br />}<br />}<br />?></code></p>