> 백엔드 개발 > PHP 튜토리얼 > PHP验证表单的具体实现方式_PHP教程

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

WBOY
풀어 주다: 2016-07-15 13:29:32
원래의
919명이 탐색했습니다.

学习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("...
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿