首页 >后端开发 >php教程 >php表单数据验证类,php表单验证_PHP教程

php表单数据验证类,php表单验证_PHP教程

WBOY
WBOY原创
2016-07-13 09:45:361034浏览

php表单数据验证类,php表单验证

非常好用方便的表单数据验证类

<?<span>php 
</span><span>//</span><span>验证类</span>
<span>class</span><span> Fun{ 
       
    </span><span>function</span> isEmpty(<span>$val</span><span>) 
    { 
        </span><span>if</span> (!<span>is_string</span>(<span>$val</span>)) <span>return</span> <span>false</span>; <span>//</span><span>是否是字符串类型  </span>
   
        <span>if</span> (<span>empty</span>(<span>$val</span>)) <span>return</span> <span>false</span>; <span>//</span><span>是否已设定  </span>
   
        <span>if</span> (<span>$val</span>=='') <span>return</span> <span>false</span>; <span>//</span><span>是否为空  </span>
   
        <span>return</span> <span>true</span><span>;  
           
    } 
    </span><span>/*</span><span>
     -----------------------------------------------------------
    函数名称:isNumber
    简要描述:检查输入的是否为数字
    输入:string
    输出:boolean
    修改日志:------
    -----------------------------------------------------------
    </span><span>*/</span> 
    <span>function</span> isNumber(<span>$val</span><span>) 
    { 
        </span><span>if</span>(<span>ereg</span>("^[0-9]+$", <span>$val</span><span>)) 
            </span><span>return</span> <span>true</span><span>; 
        </span><span>return</span> <span>false</span><span>; 
    } 
        
    </span><span>/*</span><span>
     -----------------------------------------------------------
    函数名称:isPhone
    简要描述:检查输入的是否为电话
    输入:string
    输出:boolean
    修改日志:------
    -----------------------------------------------------------
    </span><span>*/</span> 
    <span>function</span> isPhone(<span>$val</span><span>) 
    { 
        </span><span>//</span><span>eg: xxx-xxxxxxxx-xxx | xxxx-xxxxxxx-xxx ... </span>
        <span>if</span>(<span>ereg</span>("^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$",<span>$val</span><span>)) 
            </span><span>return</span> <span>true</span><span>; 
        </span><span>return</span> <span>false</span><span>; 
    } 
   
    </span><span>/*</span><span>
     -----------------------------------------------------------
    函数名称:isPostcode
    简要描述:检查输入的是否为邮编
    输入:string
    输出:boolean
    修改日志:------
    -----------------------------------------------------------
    </span><span>*/</span> 
    <span>function</span> isPostcode(<span>$val</span><span>) 
    { 
        </span><span>if</span>(<span>ereg</span>("^[0-9]{4,6}$",<span>$val</span><span>)) 
            </span><span>return</span> <span>true</span><span>; 
        </span><span>return</span> <span>false</span><span>; 
    } 
        
    </span><span>/*</span><span>
     -----------------------------------------------------------
    函数名称:isEmail
    简要描述:邮箱地址合法性检查
    输入:string
    输出:boolean
    修改日志:------
    -----------------------------------------------------------
    </span><span>*/</span> 
    <span>function</span> isEmail(<span>$val</span>,<span>$domain</span>=""<span>) 
    { 
        </span><span>if</span>(!<span>$domain</span><span>) 
        { 
            </span><span>if</span>( <span>preg_match</span>("/^[a-z0-9-_.]+@[\da-z][\.\w-]+\.[a-z]{2,4}$/i", <span>$val</span><span>) ) 
            { 
                </span><span>return</span> <span>true</span><span>; 
            } 
            </span><span>else</span> 
                <span>return</span> <span>false</span><span>; 
        } 
        </span><span>else</span><span> 
        { 
            </span><span>if</span>( <span>preg_match</span>("/^[a-z0-9-_.]+@".<span>$domain</span>."$/i", <span>$val</span><span>) ) 
            { 
                </span><span>return</span> <span>true</span><span>; 
            } 
            </span><span>else</span> 
                <span>return</span> <span>false</span><span>; 
        } 
    }</span><span>//</span><span>end func </span>
        
    <span>/*</span><span>
     -----------------------------------------------------------
    函数名称:isName
    简要描述:姓名昵称合法性检查,只能输入中文英文
    输入:string
    输出:boolean
    修改日志:------
    -----------------------------------------------------------
    </span><span>*/</span> 
    <span>function</span> isName(<span>$val</span><span>) 
    { 
        </span><span>if</span>( <span>preg_match</span>("/^[\x80-\xffa-zA-Z0-9]{3,60}$/", <span>$val</span>) )<span>//</span><span>2008-7-24 </span>
<span>        { 
            </span><span>return</span> <span>true</span><span>; 
        } 
        </span><span>return</span> <span>false</span><span>; 
    }</span><span>//</span><span>end func </span>
        
       
    <span>/*</span><span>
     -----------------------------------------------------------
    函数名称:isStrLength($theelement, $min, $max)
    简要描述:检查字符串长度是否符合要求
    输入:mixed (字符串,最小长度,最大长度)
    输出:boolean
    修改日志:------
    -----------------------------------------------------------
    </span><span>*/</span> 
    <span>function</span> isStrLength(<span>$val</span>, <span>$min</span>, <span>$max</span><span>) 
    { 
        </span><span>$theelement</span>= <span>trim</span>(<span>$val</span><span>); 
        </span><span>if</span>(<span>ereg</span>("^[a-zA-Z0-9]{".<span>$min</span>.",".<span>$max</span>."}$",<span>$val</span><span>)) 
            </span><span>return</span> <span>true</span><span>; 
        </span><span>return</span> <span>false</span><span>; 
    } 
       
        
    </span><span>/*</span><span>
     -----------------------------------------------------------
    函数名称:isNumberLength($theelement, $min, $max)
    简要描述:检查字符串长度是否符合要求
    输入:mixed (字符串,最小长度,最大长度)
    输出:boolean
    修改日志:------
    -----------------------------------------------------------
    </span><span>*/</span> 
    <span>function</span> isNumLength(<span>$val</span>, <span>$min</span>, <span>$max</span><span>) 
    { 
        </span><span>$theelement</span>= <span>trim</span>(<span>$val</span><span>); 
        </span><span>if</span>(<span>ereg</span>("^[0-9]{".<span>$min</span>.",".<span>$max</span>."}$",<span>$val</span><span>)) 
            </span><span>return</span> <span>true</span><span>; 
        </span><span>return</span> <span>false</span><span>; 
    } 
        
    </span><span>/*</span><span>
     -----------------------------------------------------------
    函数名称:isNumberLength($theelement, $min, $max)
    简要描述:检查字符串长度是否符合要求
    输入:mixed (字符串,最小长度,最大长度)
    输出:boolean
    修改日志:------
    -----------------------------------------------------------
    </span><span>*/</span> 
    <span>function</span> isEngLength(<span>$val</span>, <span>$min</span>, <span>$max</span><span>) 
    { 
        </span><span>$theelement</span>= <span>trim</span>(<span>$val</span><span>); 
        </span><span>if</span>(<span>ereg</span>("^[a-zA-Z]{".<span>$min</span>.",".<span>$max</span>."}$",<span>$val</span><span>)) 
            </span><span>return</span> <span>true</span><span>; 
        </span><span>return</span> <span>false</span><span>; 
    } 
        
    </span><span>/*</span><span>
     -----------------------------------------------------------
    函数名称:isEnglish
    简要描述:检查输入是否为英文
    输入:string
    输出:boolean
    作者:------
    修改日志:------
    -----------------------------------------------------------
    </span><span>*/</span> 
    <span>function</span> isEnglish(<span>$theelement</span><span>) 
    { 
        </span><span>if</span>( <span>ereg</span>("[\x80-\xff].",<span>$theelement</span><span>) ) 
        { 
            </span><span>Return</span> <span>false</span><span>; 
        } 
        </span><span>Return</span> <span>true</span><span>; 
    } 
        
    </span><span>/*</span><span>
     -----------------------------------------------------------
    函数名称:isChinese
    简要描述:检查是否输入为汉字
    输入:string
    输出:boolean
    修改日志:------
    -----------------------------------------------------------
    </span><span>*/</span> 
    <span>/*</span><span>
    function isChinese($sInBuf)//有问题的函数
    {
        $iLen= strlen($sInBuf);
        for($i= 0; $i< $iLen; $i++)
        {
        if(ord($sInBuf{$i})>=0x80)
            {
            if( (ord($sInBuf{$i})>=0x81 && ord($sInBuf{$i})<=0xFE) && ((ord($sInBuf{$i+1})>=0x40 && ord($sInBuf{$i+1}) < 0x7E) || (ord($sInBuf{$i+1}) > 0x7E && ord($sInBuf{$i+1})<=0xFE)) )
            {
            if(ord($sInBuf{$i})>0xA0 && ord($sInBuf{$i})<0xAA)
                {
                    //有中文标点
                        return false;
            }
            }
            else
            {
            //有日文或其它文字
            return false;
            }
            $i++;
            }
                else
                    {
                        return false;
            }
            }
            return true;
        }</span><span>*/</span> 
           
           
        <span>function</span> isChinese(<span>$sInBuf</span>)<span>//</span><span>正确的函数 </span>
<span>        { 
            </span><span>if</span> (<span>preg_match</span>("/^[\x7f-\xff]+$/", <span>$sInBuf</span>)) { <span>//</span><span>兼容gb2312,utf-8 </span>
                   
                <span>return</span> <span>true</span><span>; 
            } 
            </span><span>else</span><span> 
            { 
                </span><span>return</span> <span>false</span><span>; 
            } 
        } 
            </span><span>/*</span><span>
             -----------------------------------------------------------
            函数名称:isDomain($Domain)
            简要描述:检查一个(英文)域名是否合法
            输入:string 域名
            输出:boolean
            修改日志:------
            -----------------------------------------------------------
            </span><span>*/</span> 
            <span>function</span> isDomain(<span>$Domain</span><span>) 
            { 
                </span><span>if</span>(!<span>eregi</span>("^[0-9a-z]+[0-9a-z\.-]+[0-9a-z]+$", <span>$Domain</span><span>)) 
                { 
                    </span><span>Return</span> <span>false</span><span>; 
                } 
                </span><span>if</span>( !<span>eregi</span>("\.", <span>$Domain</span><span>)) 
                { 
                    </span><span>Return</span> <span>false</span><span>; 
                } 
                    
                </span><span>if</span>(<span>eregi</span>("\-\.", <span>$Domain</span>) or <span>eregi</span>("\-\-", <span>$Domain</span>) or <span>eregi</span>("\.\.", <span>$Domain</span>) or <span>eregi</span>("\.\-", <span>$Domain</span><span>)) 
                { 
                    </span><span>Return</span> <span>false</span><span>; 
                } 
                    
                </span><span>$aDomain</span>= <span>explode</span>(".",<span>$Domain</span><span>); 
                </span><span>if</span>( !<span>eregi</span>("[a-zA-Z]",<span>$aDomain</span>[<span>count</span>(<span>$aDomain</span>)-1<span>]) ) 
                { 
                    </span><span>Return</span> <span>false</span><span>; 
                } 
                    
                </span><span>if</span>(<span>strlen</span>(<span>$aDomain</span>[0]) > 63 || <span>strlen</span>(<span>$aDomain</span>[0]) < 1<span>) 
                { 
                    </span><span>Return</span> <span>false</span><span>; 
                } 
                </span><span>Return</span> <span>true</span><span>; 
            } 
            </span><span>/*</span><span>*
             * 验证是否日期的函数
             * @param unknown_type $date
             * @param unknown_type $format
             * @throws Exception
             * @return boolean
             </span><span>*/</span> 
            <span>function</span> validateDate( <span>$date</span>, <span>$format</span>='YYYY-MM-DD'<span>) 
            { 
                </span><span>switch</span>( <span>$format</span><span> ) 
                { 
                    </span><span>case</span> 'YYYY/MM/DD': 
                    <span>case</span> 'YYYY-MM-DD': 
                        <span>list</span>( <span>$y</span>, <span>$m</span>, <span>$d</span> ) = <span>preg_split</span>( '/[-./ ]/', <span>$date</span><span> ); 
                        </span><span>break</span><span>; 
               
                    </span><span>case</span> 'YYYY/DD/MM': 
                    <span>case</span> 'YYYY-DD-MM': 
                        <span>list</span>( <span>$y</span>, <span>$d</span>, <span>$m</span> ) = <span>preg_split</span>( '/[-./ ]/', <span>$date</span><span> ); 
                        </span><span>break</span><span>; 
               
                    </span><span>case</span> 'DD-MM-YYYY': 
                    <span>case</span> 'DD/MM/YYYY': 
                        <span>list</span>( <span>$d</span>, <span>$m</span>, <span>$y</span> ) = <span>preg_split</span>( '/[-./ ]/', <span>$date</span><span> ); 
                        </span><span>break</span><span>; 
               
                    </span><span>case</span> 'MM-DD-YYYY': 
                    <span>case</span> 'MM/DD/YYYY': 
                        <span>list</span>( <span>$m</span>, <span>$d</span>, <span>$y</span> ) = <span>preg_split</span>( '/[-./ ]/', <span>$date</span><span> ); 
                        </span><span>break</span><span>; 
               
                    </span><span>case</span> 'YYYYMMDD': 
                        <span>$y</span> = <span>substr</span>( <span>$date</span>, 0, 4<span> ); 
                        </span><span>$m</span> = <span>substr</span>( <span>$date</span>, 4, 2<span> ); 
                        </span><span>$d</span> = <span>substr</span>( <span>$date</span>, 6, 2<span> ); 
                        </span><span>break</span><span>; 
               
                    </span><span>case</span> 'YYYYDDMM': 
                        <span>$y</span> = <span>substr</span>( <span>$date</span>, 0, 4<span> ); 
                        </span><span>$d</span> = <span>substr</span>( <span>$date</span>, 4, 2<span> ); 
                        </span><span>$m</span> = <span>substr</span>( <span>$date</span>, 6, 2<span> ); 
                        </span><span>break</span><span>; 
               
                    </span><span>default</span>: 
                        <span>throw</span> <span>new</span> <span>Exception</span>( "Invalid Date Format"<span> ); 
                } 
                </span><span>return</span> <span>checkdate</span>( <span>$m</span>, <span>$d</span>, <span>$y</span><span> ); 
            } 
               
               
            </span><span>/*</span><span>
            -----------------------------------------------------------
            函数名称:isDate
            简要描述:检查日期是否符合0000-00-00
                输入:string
                    输出:boolean
                    修改日志:------
                    -----------------------------------------------------------
                    </span><span>*/</span> 
                    <span>function</span> isDate(<span>$sDate</span><span>) 
                    { 
                        </span><span>if</span>( <span>ereg</span>("^[0-9]{4}\-[][0-9]{2}\-[0-9]{2}$",<span>$sDate</span><span>) ) 
                        { 
                        </span><span>Return</span> <span>true</span><span>; 
            } 
            </span><span>else</span><span> 
            { 
            </span><span>Return</span> <span>false</span><span>; 
        } 
        } 
         </span><span>/*</span><span>
          -----------------------------------------------------------
          函数名称:isTime
          简要描述:检查日期是否符合0000-00-00 00:00:00
          输入:string
          输出:boolean
          修改日志:------
          -----------------------------------------------------------
          </span><span>*/</span> 
          <span>function</span> isTime(<span>$sTime</span><span>) 
          { 
         </span><span>if</span>( <span>ereg</span>("^[0-9]{4}\-[][0-9]{2}\-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$",<span>$sTime</span><span>) ) 
            { 
                </span><span>Return</span> <span>true</span><span>; 
         } 
            </span><span>else</span><span> 
            { 
            </span><span>Return</span> <span>false</span><span>; 
         } 
         } 
             
         </span><span>/*</span><span>
         -----------------------------------------------------------
         函数名称:isMoney($val)
         简要描述:检查输入值是否为合法人民币格式
         输入:string
         输出:boolean
         修改日志:------
         -----------------------------------------------------------
         </span><span>*/</span> 
         <span>function</span> isMoney(<span>$val</span><span>) 
         { 
         </span><span>if</span>(<span>ereg</span>("^[0-9]{1,}$", <span>$val</span><span>)) 
            </span><span>return</span> <span>true</span><span>; 
         </span><span>if</span>( <span>ereg</span>("^[0-9]{1,}\.[0-9]{1,2}$", <span>$val</span><span>) ) 
            </span><span>return</span> <span>true</span><span>; 
         </span><span>return</span> <span>false</span><span>; 
        } 
            
        </span><span>/*</span><span>
         -----------------------------------------------------------
        函数名称:isIp($val)
            简要描述:检查输入IP是否符合要求
            输入:string
                输出:boolean
                修改日志:------
                -----------------------------------------------------------
                </span><span>*/</span> 
                <span>function</span> isIp(<span>$val</span><span>) 
                { 
                 </span><span>return</span>(bool) <span>ip2long</span>(<span>$val</span><span>); 
                } 
                </span><span>//</span><span>----------------------------------------------------------------------------- </span>
       
       
       
        <span>/*</span><span>*
         * 验证手机号
         * @param int $mobile
         </span><span>*/</span> 
        <span>function</span> valid_mobile(<span>$mobile</span><span>){ 
            </span><span>if</span>(<span>strlen</span>(<span>$mobile</span>)!=11) <span>return</span> <span>false</span><span>; 
            </span><span>if</span>(<span>preg_match</span>('/13[0-9]\d{8}|15[0|1|2|3|5|6|7|8|9]\d{8}|18[0|5|6|7|8|9]\d{8}/',<span>$mobile</span><span>)){ 
                </span><span>return</span> <span>true</span><span>; 
            }</span><span>else</span><span>{ 
                </span><span>return</span> <span>false</span><span>; 
            } 
        } 
           
        </span><span>/*</span><span>*
         * 缩略图生成函数,最好使用GD2
         *
         * @param string $srcFile 要生成缩略图的文件
         * @param int $toW 缩略图宽度
         * @param int $toH 缩略图高度
         * @param string $toFile 缩略图文件
         </span><span>*/</span> 
        <span>function</span> ImageResize(<span>$srcFile</span>,<span>$toW</span>,<span>$toH</span>,<span>$toFile</span>=""<span>){ 
            </span><span>if</span>(<span>$toFile</span>==""){ <span>$toFile</span> = <span>$srcFile</span><span>; } 
            </span><span>$info</span> = ""<span>; 
            </span><span>$data</span> = <span>GetImageSize</span>(<span>$srcFile</span>,<span>$info</span><span>); 
            </span><span>switch</span> (<span>$data</span>[2<span>]){ 
                </span><span>case</span> 1: 
                    <span>if</span>(!<span>function_exists</span>("imagecreatefromgif"<span>)){ 
                        </span><span>//</span><span>echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!<a href='javascript:go(-1);'>返回</a>"; </span>
                        <span>return</span> <span>false</span><span>; 
                    } 
                    </span><span>$im</span> = ImageCreateFromGIF(<span>$srcFile</span><span>); 
                    </span><span>break</span><span>; 
                </span><span>case</span> 2: 
                    <span>if</span>(!<span>function_exists</span>("imagecreatefromjpeg"<span>)){ 
                        </span><span>//</span><span>echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!<a href='javascript:go(-1);'>返回</a>"; </span>
                        <span>return</span> <span>false</span><span>; 
                    } 
                    </span><span>$im</span> = ImageCreateFromJpeg(<span>$srcFile</span><span>); 
                    </span><span>break</span><span>; 
                </span><span>case</span> 3: 
                    <span>$im</span> = ImageCreateFromPNG(<span>$srcFile</span><span>); 
                    </span><span>break</span><span>; 
            } 
            </span><span>$srcW</span>=ImageSX(<span>$im</span><span>); 
            </span><span>$srcH</span>=ImageSY(<span>$im</span><span>); 
            </span><span>$toWH</span>=<span>$toW</span>/<span>$toH</span><span>; 
            </span><span>$srcWH</span>=<span>$srcW</span>/<span>$srcH</span><span>; 
            </span><span>if</span>(<span>$toWH</span><=<span>$srcWH</span><span>){ 
                </span><span>$ftoW</span>=<span>$toW</span><span>; 
                </span><span>$ftoH</span>=<span>$ftoW</span>*(<span>$srcH</span>/<span>$srcW</span><span>); 
            } 
            </span><span>else</span><span>{ 
                </span><span>$ftoH</span>=<span>$toH</span><span>; 
                </span><span>$ftoW</span>=<span>$ftoH</span>*(<span>$srcW</span>/<span>$srcH</span><span>); 
            } 
            </span><span>if</span>(<span>$srcW</span>><span>$toW</span>||<span>$srcH</span>><span>$toH</span><span>) 
            { 
                </span><span>if</span>(<span>function_exists</span>("imagecreatetruecolor"<span>)){ 
                    @</span><span>$ni</span> = ImageCreateTrueColor(<span>$ftoW</span>,<span>$ftoH</span><span>); 
                    </span><span>if</span>(<span>$ni</span>) ImageCopyResampled(<span>$ni</span>,<span>$im</span>,0,0,0,0,<span>$ftoW</span>,<span>$ftoH</span>,<span>$srcW</span>,<span>$srcH</span><span>); 
                    </span><span>else</span><span>{ 
                        </span><span>$ni</span>=ImageCreate(<span>$ftoW</span>,<span>$ftoH</span><span>); 
                        ImageCopyResized(</span><span>$ni</span>,<span>$im</span>,0,0,0,0,<span>$ftoW</span>,<span>$ftoH</span>,<span>$srcW</span>,<span>$srcH</span><span>); 
                    } 
                }</span><span>else</span><span>{ 
                    </span><span>$ni</span>=ImageCreate(<span>$ftoW</span>,<span>$ftoH</span><span>); 
                    ImageCopyResized(</span><span>$ni</span>,<span>$im</span>,0,0,0,0,<span>$ftoW</span>,<span>$ftoH</span>,<span>$srcW</span>,<span>$srcH</span><span>); 
                } 
                </span><span>if</span>(<span>function_exists</span>('imagejpeg')) ImageJpeg(<span>$ni</span>,<span>$toFile</span><span>); 
                </span><span>else</span> ImagePNG(<span>$ni</span>,<span>$toFile</span><span>); 
                ImageDestroy(</span><span>$ni</span><span>); 
            }</span><span>else</span><span>{ 
                ImageDestroy(</span><span>$im</span><span>); 
                </span><span>return</span> <span>false</span><span>; 
            } 
            ImageDestroy(</span><span>$im</span><span>); 
            </span><span>return</span> <span>true</span><span>; 
        } 
           
           
           
        </span><span>//</span><span>去除字符串空格 </span>
        <span>static</span> <span>function</span> strTrim(<span>$str</span><span>) 
        { 
            </span><span>return</span> <span>preg_replace</span>("/\s/","",<span>$str</span><span>); 
        } 
           
        </span><span>//</span><span>验证用户名 </span>
        <span>static</span> <span>function</span> userName(<span>$str</span>,<span>$type</span>,<span>$len</span><span>) 
        { 
            </span><span>$str</span>=self::strTrim(<span>$str</span><span>); 
            </span><span>if</span>(<span>$len</span><<span>strlen</span>(<span>$str</span><span>)) 
            { 
                </span><span>return</span> <span>false</span><span>; 
            }</span><span>else</span><span>{ 
                </span><span>switch</span>(<span>$type</span><span>) 
                { 
                    </span><span>case</span> "EN":<span>//</span><span>纯英文 </span>
                        <span>if</span>(<span>preg_match</span>("/^[a-zA-Z]+$/",<span>$str</span><span>)) 
                        { 
                            </span><span>return</span> <span>true</span><span>; 
                        }</span><span>else</span><span>{ 
                            </span><span>return</span> <span>false</span><span>; 
                        } 
                        </span><span>break</span><span>; 
                    </span><span>case</span> "ENNUM":<span>//</span><span>英文数字 </span>
                        <span>if</span>(<span>preg_match</span>("/^[a-zA-Z0-9]+$/",<span>$str</span><span>)) 
                        { 
                            </span><span>return</span> <span>true</span><span>; 
                        }</span><span>else</span><span>{ 
                            </span><span>return</span> <span>false</span><span>; 
                        } 
                        </span><span>break</span><span>; 
                    </span><span>case</span> "ALL":        <span>//</span><span>允许的符号(|-_字母数字) </span>
                        <span>if</span>(<span>preg_match</span>("/^[\|\-\_a-zA-Z0-9]+$/",<span>$str</span><span>)) 
                        { 
                            </span><span>return</span> <span>true</span><span>; 
                        }</span><span>else</span><span>{ 
                            </span><span>return</span> <span>false</span><span>; 
                        } 
                        </span><span>break</span><span>; 
                } 
            } 
        } 
           
        </span><span>//</span><span>验证密码长度 </span>
        <span>static</span> <span>function</span> passWord(<span>$min</span>,<span>$max</span>,<span>$str</span><span>) 
        { 
            </span><span>$str</span>=self::strTrim(<span>$str</span><span>); 
            </span><span>if</span>(<span>strlen</span>(<span>$str</span>)>=<span>$min</span> && <span>strlen</span>(<span>$str</span>)<=<span>$max</span><span>) 
            { 
                </span><span>return</span> <span>true</span><span>; 
            }</span><span>else</span><span>{ 
                </span><span>return</span> <span>false</span><span>; 
            } 
        } 
           
        </span><span>//</span><span>验证Email </span>
        <span>static</span> <span>function</span> Email(<span>$str</span><span>) 
        { 
            </span><span>$str</span>=self::strTrim(<span>$str</span><span>); 
                
            </span><span>if</span>(<span>preg_match</span>("/^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.){1,2}[a-z]{2,4}$/i",<span>$str</span><span>)) 
            { 
                </span><span>return</span> <span>true</span><span>; 
            }</span><span>else</span><span>{ 
                </span><span>return</span> <span>false</span><span>; 
            } 
                
        } 
           
        </span><span>//</span><span>验证身份证(中国) </span>
        <span>static</span> <span>function</span> idCard(<span>$str</span><span>) 
        { 
            </span><span>$str</span>=self::strTrim(<span>$str</span><span>); 
            </span><span>if</span>(<span>preg_match</span>("/^([0-9]{15}|[0-9]{17}[0-9a-z])$/i",<span>$str</span><span>)) 
            { 
                </span><span>return</span> <span>true</span><span>; 
            }</span><span>else</span><span>{ 
                </span><span>return</span> <span>false</span><span>; 
            } 
        } 
           
        </span><span>//</span><span>验证座机电话 </span>
        <span>static</span> <span>function</span> Phone(<span>$type</span>,<span>$str</span><span>) 
        { 
            </span><span>$str</span>=self::strTrim(<span>$str</span><span>); 
            </span><span>switch</span>(<span>$type</span><span>) 
            { 
                </span><span>case</span> "CHN": 
                    <span>if</span>(<span>preg_match</span>("/^([0-9]{3}|0[0-9]{3})-[0-9]{7,8}$/",<span>$str</span><span>)) 
                    { 
                        </span><span>return</span> <span>true</span><span>; 
                    }</span><span>else</span><span>{ 
                        </span><span>return</span> <span>false</span><span>; 
                    } 
                    </span><span>break</span><span>; 
                </span><span>case</span> "INT": 
                    <span>if</span>(<span>preg_match</span>("/^[0-9]{4}-([0-9]{3}|0[0-9]{3})-[0-9]{7,8}$/",<span>$str</span><span>)) 
                    { 
                        </span><span>return</span> <span>true</span><span>; 
                    }</span><span>else</span><span>{ 
                        </span><span>return</span> <span>false</span><span>; 
                    } 
                    </span><span>break</span><span>; 
            } 
        } 
   
        </span><span>/*</span><span>*
         * 过滤二维数组的值
         * @param 二维数组 $arr_data
         * @param 一维数组 $field
         * @return Ambigous <multitype:, string, unknown>
         </span><span>*/</span> 
        <span>function</span> getarrayfield(<span>$arr_data</span>, <span>$field</span><span>) 
        { 
            </span><span>$resultArr</span> = <span>array</span><span>(); 
            </span><span>foreach</span> ( <span>$arr_data</span> <span>as</span> <span>$key</span> => <span>$value</span><span> ) { 
                </span><span>foreach</span> (<span>$field</span> <span>as</span> <span>$k</span>=><span>$v</span><span>) 
                { 
                    </span><span>if</span>(<span>array_key_exists</span>(<span>$v</span>, <span>$value</span>))<span>//</span><span>存在才添加 </span>
<span>                    { 
                        </span><span>$resultArr</span>[<span>$key</span>][<span>$v</span>] =<span>$value</span>[<span>$v</span><span>] ; 
                    } 
                    </span><span>else</span><span> 
                    { 
                        </span><span>$resultArr</span>[<span>$key</span>][<span>$v</span>]="不存在这个列"<span>; 
                    } 
                } 
            } 
                   
            </span><span>return</span> <span>$resultArr</span><span>; 
        } 
        </span><span>/*</span><span>*
         * 获取客户端IP地址
         * @return ip
         </span><span>*/</span> 
        <span>function</span><span> get_client_ip(){ 
            </span><span>if</span> (<span>getenv</span>("HTTP_CLIENT_IP") && <span>strcasecmp</span>(<span>getenv</span>("HTTP_CLIENT_IP"), "unknown"<span>)) 
                </span><span>$ip</span> = <span>getenv</span>("HTTP_CLIENT_IP"<span>); 
            </span><span>else</span> <span>if</span> (<span>getenv</span>("HTTP_X_FORWARDED_FOR") && <span>strcasecmp</span>(<span>getenv</span>("HTTP_X_FORWARDED_FOR"), "unknown"<span>)) 
                </span><span>$ip</span> = <span>getenv</span>("HTTP_X_FORWARDED_FOR"<span>); 
            </span><span>else</span> <span>if</span> (<span>getenv</span>("REMOTE_ADDR") && <span>strcasecmp</span>(<span>getenv</span>("REMOTE_ADDR"), "unknown"<span>)) 
                </span><span>$ip</span> = <span>getenv</span>("REMOTE_ADDR"<span>); 
            </span><span>else</span> <span>if</span> (<span>isset</span>(<span>$_SERVER</span>['REMOTE_ADDR']) && <span>$_SERVER</span>['REMOTE_ADDR'] && <span>strcasecmp</span>(<span>$_SERVER</span>['REMOTE_ADDR'], "unknown"<span>)) 
                </span><span>$ip</span> = <span>$_SERVER</span>['REMOTE_ADDR'<span>]; 
            </span><span>else</span> 
                <span>$ip</span> = "unknown"<span>; 
            </span><span>return</span>(<span>$ip</span><span>); 
        } 
           
        </span><span>function</span><span> get_http_user_agent(){ 
            </span><span>return</span> <span>isset</span>(<span>$_SERVER</span>['HTTP_USER_AGENT']) ? <span>$_SERVER</span>['HTTP_USER_AGENT'] : ""<span>; 
        } 
           
        </span><span>/*</span><span>*
         * 从IP地址获取真实地址
         * @param IP $ip
         </span><span>*/</span> 
        <span>function</span> get_address_from_ip(<span>$ip</span><span>){ 
            </span><span>$url</span>='http://www.youdao.com/smartresult-xml/search.s?type=ip&q='<span>; 
            </span><span>$xml</span>=<span>file_get_contents</span>(<span>$url</span>.<span>$ip</span><span>); 
            </span><span>$data</span>=<span>simplexml_load_string</span>(<span>$xml</span><span>); 
            </span><span>return</span> <span>$data</span>->product-><span>location; 
        } 
   
           
        </span><span>/*</span><span>*
         * 产生随机字串,可用来自动生成密码 默认长度6位 字母和数字混合
         * @param string $len 长度
         * @param string $type 字串类型
         * 0 字母 1 数字 2 大写字母 3 小写字母 默认混合 4中文
         * @param string $addChars 额外字符
         * @return string
         </span><span>*/</span> 
        <span>function</span> rand_string(<span>$len</span>=6,<span>$type</span>='',<span>$addChars</span>=''<span>) { 
            </span><span>$str</span> =''<span>; 
            </span><span>switch</span>(<span>$type</span><span>) { 
                </span><span>case</span> 0: 
                    <span>$chars</span>='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.<span>$addChars</span><span>; 
                    </span><span>break</span><span>; 
                </span><span>case</span> 1: 
                    <span>$chars</span>= <span>str_repeat</span>('0123456789',3<span>); 
                    </span><span>break</span><span>; 
                </span><span>case</span> 2: 
                    <span>$chars</span>='ABCDEFGHIJKLMNOPQRSTUVWXYZ'.<span>$addChars</span><span>; 
                    </span><span>break</span><span>; 
                </span><span>case</span> 3: 
                    <span>$chars</span>='abcdefghijklmnopqrstuvwxyz'.<span>$addChars</span><span>; 
                    </span><span>break</span><span>; 
                </span><span>case</span> 4: 
                    <span>$chars</span> = "们以我到他会作时要动国产的一是工就年阶义发成部民可出能方进在了不和有大这主中人上为来分生对于学下级地个用同行面说种过命度革而多子后自社加小机也经力线本电高量长党得实家定深法表着水理化争现所二起政三好十战无农使性前等反体合斗路图把结第里正新开论之物从当两些还天资事队批点育重其思与间内去因件日利相由压员气业代全组数果期导平各基或月毛然如应形想制心样干都向变关问比展那它最及外没看治提五解系林者米群头意只明四道马认次文通但条较克又公孔领军流入接席位情运器并飞原油放立题质指建区验活众很教决特此常石强极土少已根共直团统式转别造切九你取西持总料连任志观调七么山程百报更见必真保热委手改管处己将修支识病象几先老光专什六型具示复安带每东增则完风回南广劳轮科北打积车计给节做务被整联步类集号列温装即毫知轴研单色坚据速防史拉世设达尔场织历花受求传口断况采精金界品判参层止边清至万确究书术状厂须离再目海交权且儿青才证低越际八试规斯近注办布门铁需走议县兵固除般引齿千胜细影济白格效置推空配刀叶率述今选养德话查差半敌始片施响收华觉备名红续均药标记难存测士身紧液派准斤角降维板许破述技消底床田势端感往神便贺村构照容非搞亚磨族火段算适讲按值美态黄易彪服早班麦削信排台声该击素张密害侯草何树肥继右属市严径螺检左页抗苏显苦英快称坏移约巴材省黑武培著河帝仅针怎植京助升王眼她抓含苗副杂普谈围食射源例致酸旧却充足短划剂宣环落首尺波承粉践府鱼随考刻靠够满夫失包住促枝局菌杆周护岩师举曲春元超负砂封换太模贫减阳扬江析亩木言球朝医校古呢稻宋听唯输滑站另卫字鼓刚写刘微略范供阿块某功套友限项余倒卷创律雨让骨远帮初皮播优占死毒圈伟季训控激找叫云互跟裂粮粒母练塞钢顶策双留误础吸阻故寸盾晚丝女散焊功株亲院冷彻弹错散商视艺灭版烈零室轻血倍缺厘泵察绝富城冲喷壤简否柱李望盘磁雄似困巩益洲脱投送奴侧润盖挥距触星松送获兴独官混纪依未突架宽冬章湿偏纹吃执阀矿寨责熟稳夺硬价努翻奇甲预职评读背协损棉侵灰虽矛厚罗泥辟告卵箱掌氧恩爱停曾溶营终纲孟钱待尽俄缩沙退陈讨奋械载胞幼哪剥迫旋征槽倒握担仍呀鲜吧卡粗介钻逐弱脚怕盐末阴丰雾冠丙街莱贝辐肠付吉渗瑞惊顿挤秒悬姆烂森糖圣凹陶词迟蚕亿矩康遵牧遭幅园腔订香肉弟屋敏恢忘编印蜂急拿扩伤飞露核缘游振操央伍域甚迅辉异序免纸夜乡久隶缸夹念兰映沟乙吗儒杀汽磷艰晶插埃燃欢铁补咱芽永瓦倾阵碳演威附牙芽永瓦斜灌欧献顺猪洋腐请透司危括脉宜笑若尾束壮暴企菜穗楚汉愈绿拖牛份染既秋遍锻玉夏疗尖殖井费州访吹荣铜沿替滚客召旱悟刺脑措贯藏敢令隙炉壳硫煤迎铸粘探临薄旬善福纵择礼愿伏残雷延烟句纯渐耕跑泽慢栽鲁赤繁境潮横掉锥希池败船假亮谓托伙哲怀割摆贡呈劲财仪沉炼麻罪祖息车穿货销齐鼠抽画饲龙库守筑房歌寒喜哥洗蚀废纳腹乎录镜妇恶脂庄擦险赞钟摇典柄辩竹谷卖乱虚桥奥伯赶垂途额壁网截野遗静谋弄挂课镇妄盛耐援扎虑键归符庆聚绕摩忙舞遇索顾胶羊湖钉仁音迹碎伸灯避泛亡答勇频皇柳哈揭甘诺概宪浓岛袭谁洪谢炮浇斑讯懂灵蛋闭孩释乳巨徒私银伊景坦累匀霉杜乐勒隔弯绩招绍胡呼痛峰零柴簧午跳居尚丁秦稍追梁折耗碱殊岗挖氏刃剧堆赫荷胸衡勤膜篇登驻案刊秧缓凸役剪川雪链渔啦脸户洛孢勃盟买杨宗焦赛旗滤硅炭股坐蒸凝竟陷枪黎救冒暗洞犯筒您宋弧爆谬涂味津臂障褐陆啊健尊豆拔莫抵桑坡缝警挑污冰柬嘴啥饭塑寄赵喊垫丹渡耳刨虎笔稀昆浪萨茶滴浅拥穴覆伦娘吨浸袖珠雌妈紫戏塔锤震岁貌洁剖牢锋疑霸闪埔猛诉刷狠忽灾闹乔唐漏闻沈熔氯荒茎男凡抢像浆旁玻亦忠唱蒙予纷捕锁尤乘乌智淡允叛畜俘摸锈扫毕璃宝芯爷鉴秘净蒋钙肩腾枯抛轨堂拌爸循诱祝励肯酒绳穷塘燥泡袋朗喂铝软渠颗惯贸粪综墙趋彼届墨碍启逆卸航衣孙龄岭骗休借".<span>$addChars</span><span>; 
                    </span><span>break</span><span>; 
                </span><span>case</span> 5: 
                    <span>$chars</span>='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.<span>$addChars</span><span>; 
                    </span><span>break</span><span>; 
                </span><span>default</span> : 
                    <span>//</span><span> 默认去掉了容易混淆的字符oOLl和数字01,要添加请使用addChars参数 </span>
                    <span>$chars</span>='ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789'.<span>$addChars</span><span>; 
                    </span><span>break</span><span>; 
            } 
            </span><span>if</span>(<span>$len</span>>10 ) {<span>//</span><span>位数过长重复字符串一定次数 </span>
                <span>$chars</span>= <span>$type</span>==1? <span>str_repeat</span>(<span>$chars</span>,<span>$len</span>) : <span>str_repeat</span>(<span>$chars</span>,5<span>); 
            } 
           
            </span><span>if</span>(<span>$type</span> == 4){ <span>//</span><span> 处理中文 </span>
                <span>$chars</span> = <span>preg_replace</span>('/[^\\x{4e00}-\\x{9fa5}]/u', '', <span>$chars</span>);     <span>//</span><span> //干掉非utf8中文字符 </span>
                <span>$chars</span> = <span>chunk_split</span>(<span>$chars</span>,3,","); <span>//</span><span> //每隔3个字符插入一个&ldquo;,&rdquo;,转换为数组使用,使用strlen()测出php中一个中文霸占了3个 </span>
                <span>$re</span> = <span>explode</span>(",",<span>$chars</span><span>); 
                </span><span>shuffle</span>(<span>$re</span>);<span>//</span><span>随机重新排序数组 </span>
                <span>$chars</span> = <span>implode</span>(<span>$re</span><span>); 
                </span><span>unset</span>(<span>$re</span><span>); 
                </span><span>$str</span>     =   mb_substr(<span>$chars</span>,0,<span>$len</span>,"utf-8"<span>); 
            }</span><span>else</span><span>{ 
                </span><span>$chars</span>   =   <span>str_shuffle</span>(<span>$chars</span><span>); 
                </span><span>$str</span>     =   <span>substr</span>(<span>$chars</span>,0,<span>$len</span><span>); 
            } 
           
            </span><span>return</span> <span>$str</span><span>; 
        } 
           
        </span><span>//</span><span>获取url中参数的值 </span>
        <span>function</span> geturlval(<span>$url</span>,<span>$name</span><span>) 
        { 
            </span><span>$arr</span> = <span>parse_url</span>(<span>$url</span><span>); 
            </span><span>$arr_query</span> = <span>$this</span>->convertUrlQuery(<span>$arr</span>['query'<span>]); 
                   
            </span><span>return</span> <span>$arr_query</span>[<span>$name</span><span>]; 
           
        } 
        </span><span>function</span> convertUrlQuery(<span>$query</span><span>) 
        { 
            </span><span>$queryParts</span> = <span>explode</span>('&', <span>$query</span><span>); 
           
            </span><span>$params</span> = <span>array</span><span>(); 
            </span><span>foreach</span> (<span>$queryParts</span> <span>as</span> <span>$param</span><span>) 
            { 
                </span><span>$item</span> = <span>explode</span>('=', <span>$param</span><span>); 
                </span><span>$params</span>[<span>$item</span>[0]] = <span>$item</span>[1<span>]; 
            } 
           
            </span><span>return</span> <span>$params</span><span>; 
        } 
        </span><span>/*</span><span>*
         * 抓取远程图片
         *
         * @param string $url 远程图片路径
         * @param string $filename 本地存储文件名
         </span><span>*/</span> 
        <span>function</span> grabImage(<span>$url</span>, <span>$savepath</span><span>) { 
            </span><span>if</span>(<span>$url</span> ==""<span>) { 
                </span><span>return</span> <span>false</span>; <span>//</span><span>如果 $url 为空则返回 false; </span>
<span>            } 
            </span><span>$ext_name</span> = <span>strrchr</span>(<span>$url</span>, '.'); <span>//</span><span>获取图片的扩展名 </span>
            <span>if</span>(<span>$ext_name</span> != '.gif' && <span>$ext_name</span> != '.jpg' && <span>$ext_name</span> != '.bmp' && <span>$ext_name</span> != '.png'<span>) { 
                </span><span>return</span> <span>false</span>; <span>//</span><span>格式不在允许的范围 </span>
<span>            } 
            </span><span>//</span><span>获取原图片名 </span>
            <span>$filename</span> = <span>$savepath</span>.'\\'.<span>end</span>(<span>explode</span>('/',<span>$url</span><span>)); 
            </span><span>//</span><span>开始捕获 </span>
            <span>ob_start</span><span>(); 
            </span><span>readfile</span>(<span>$url</span><span>); 
            </span><span>$img_data</span> = <span>ob_get_contents</span><span>(); 
            </span><span>ob_end_clean</span><span>(); 
            </span><span>$size</span> = <span>strlen</span>(<span>$img_data</span><span>); 
            </span><span>$local_file</span> = <span>fopen</span>(<span>$filename</span> , 'a'<span>); 
            </span><span>echo</span> <span>$filename</span><span>; 
            </span><span>if</span>(<span>fwrite</span>(<span>$local_file</span>, <span>$img_data</span>)== <span>FALSE</span><span>){ 
                </span><span>echo</span> '图片下载失败'<span>; 
            } 
            </span><span>fclose</span>(<span>$local_file</span><span>); 
            </span><span>return</span> <span>$filename</span><span>; 
        } 
   
} 
</span>?>

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1040170.htmlTechArticlephp表单数据验证类,php表单验证 非常好用方便的表单数据验证类 ? php // 验证类 class Fun{ function isEmpty( $val ) { if (! is_string ( $val )) return fals...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn