首页  >  文章  >  后端开发  >  PHP验证表单的具体实现方式_PHP教程

PHP验证表单的具体实现方式_PHP教程

WBOY
WBOY原创
2016-07-15 13:29:32879浏览

学习PHP验证表单的具体代码如下:

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

以上代码就是PHP验证表单的相关实现方法。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446356.htmlTechArticle学习 PHP验证表单的具体代码如下: ? php classclass_post { //验证是否为指定长度的字母/数字组合 functionfun_text1($num1,$num2,$str) { Return(preg_match("...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn