PHP正则表达式及表单注册案例
正则表达式是一种具有特定模式的用来匹配文本的字符串 preg_match 匹配 $pattern = '/php/'; $subject = "php 是最好的编程语言,php 没有之一!"; $result = preg_match($pattern,$subject); if($result){ echo "<h1>匹配成功</h1>"; }else{ echo "<h1>匹配不成功</h1>"; } preg_match_all 匹配所有 $subject = "php是最好的编程语言,不会php的程序员不是好程序员!"preg_match_all($subject,1)">$matches); var_dump(//preg_match=== //int 1 //array (size=1) // 0 => string 'php' (length=3) //preg_match_all ==== //int 2 //array (size=1) // 0 => // array (size=2) // 0 => string 'php' (length=3) // 1 => string 'php' (length=3) preg_replace 正则替换 preg_filter() 等价于 preg_replace() ,但它仅仅返回与目标匹配的结果 $pattern = '/www..*.com/'$replacement = 'www.test.com'$subject = [ 'www.baidu.com','www.google.com','coding.test.com' ]; preg_replace($replacement,1)">array(3) { // [0] => // string(12) "www.test.com" // [1] => // string(12) "www.test.com" // [2] => // string(15) "coding.test.com" //} $result1 = preg_filter($result1array(2) { // [0] => // string(12) "www.test.com" // [1] => // string(12) "www.test.com" //} 正则搜索并使用回调替换 $pattern = '/(.*)(d{4})(-d{2}-d{2})/'$subject = "今天是2020-01-14"preg_replace_callback(function nextyear($varsreturn '明年的'.$vars[1].($vars[2]+1).$vars[3];string(28) "明年的今天是2021-01-14" } array (size=4) // 0 => string '今天是2020-01-14' (length=19) // 1 => string '今天是' (length=9) // 2 => string '2020' (length=4) // 3 => string '-01-14' (length=6) PREG_GREP_INVERT 返回没有被匹配替换到的 preg_grep(PREG_GREP_INVERT); $result);string(16) "coding.imooc.com" preg_split 按照指定规则分割字符串转数组 $pattern = '/||,|s/'$replacement = '|'$subject = "php|asp,jsp html"$result1 = preg_split(array (size=4) // 0 => string 'php' (length=3) // 1 => string 'asp' (length=3) // 2 => string 'jsp' (length=3) // 3 => string 'html' (length=4) 转义正则表达式字符 $str = '<a href="http://www.test.com">测试网</a>'preg_quote($str,'/'); $pattern = '/'.$result.'/'$subject = '<a href="http://www.test.com">测试网</a>'; echo &;a href="http://www.test.com"&;测试网&;/a&; ()单独获取 $pattern = '/[da-z]+([._-]*[da-z]+)*@[da-z]+([.-][da-z]+)*.[a-z]+/i'$subject = <<<php imooc123@qq.com //IMOOC.123@qq.com //imooc_123@qq123.com //imooc--123@qq.com //imooc123@qq.com.cn //imooc123@vip-qq.com php; array(3) { // [0] =>//打印出所有匹配到的 // array(6) { // [0] => // string(15) "imooc123@qq.com" // [1] => // string(16) "IMOOC.123@qq.com" // [2] => // string(19) "imooc_123@qq123.com" // [3] => // string(17) "imooc--123@qq.com" // [4] => // string(18) "imooc123@qq.com.cn" // [5] => // string(19) "imooc123@vip-qq.com" // } // [1] =>//打印出第一个()里的 // array(6) { // [0] => // string(0) "" // [1] => // string(4) ".123" // [2] => // string(4) "_123" // [3] => // string(5) "--123" // [4] => // string(0) "" // [5] => // string(0) "" // } // [2] =>//打印出第二个()里的 // array(6) { // [0] => // string(0) "" // [1] => // string(0) "" // [2] => // string(0) "" // [3] => // string(0) "" // [4] => // string(4) ".com" // [5] => // string(3) "-qq" // } //} ajax注册表单案例 index.html <!DOCTYPE html> <html lang="en"head> meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> link rel="stylesheet" type="text/css" href="css/base.css" ="css/register.css" title>用户注册表单验证</script src="js/jquery.js"></script> bodydiv class="wrapper container"h2>用户注册form onsubmit="return post(this)"> p ="clearfix"> label ="one">手机号码:labelinput name="telphone" class="text" placeholder="请输入手机号" /> ="error">请输入正确的手机号p="one" >校验码:="code" placeholder="请输入手机6位校验码" ="get_code"="button" value="获取验证码" >请填写手机接收到的6位数校验码> >登录密码:="password" class="请输入6-16位密码" >密码在6-16个字符之间>确认密码:="c_password"="请再次输入密码" >确认密码和密码不一致>姓名:="name"="请输入2-6位中文" >请输入2-6位中文>身份证:="idcard"="请输入18位二代身份证号码" >请输入18位二代身份证号码>邮箱:="email"="请输入Email邮箱" >Email邮箱不正确> ><="submit"="立即注册"/></formdiv> function post(obj) { var postData = $(obj).serialize(); $.post('php/check.php,{postData:postData}, (data) { if(data.code){ $("input[name=+data.sign]).siblings(.error).show(); }else{ location.href = ok.html; } },1)">json); return false; } html> ok.html charset="UTF-8">Titleh1>注册成功!> base.css /* CSS Document */ *{margin:0; padding:0;} a{text-decoration:none; cursor:pointer;} li{list-style:none;} img{border: html,body{ font-size:16px; font-family:"微软雅黑"; overflow-x:hidden;} .wrapper{width:820px; margin:0 auto;} .left{float:left;} .right{right;} .clearfix:after{content:"."; display:block; clear:both; overflow:hidden; height: .clearfix{zoom:1;} .red{color:#f93;} .mt{ margin-top:10px;} .mr{ margin-right: .mb{ margin-bottom: .ml{ margin-left:10px;} ----header----- .header{height:35px; line-height: background:#5a84d1; color:#fff;14px;} .header a{#fff;} .weixin{position:relative; width:120px;38px; text-align:center;} .wx{display:inline-block;url(../img/weixinhui.png) no-repeat;} .wx:hover{background:url(../img/weixin.png) no-repeat;} .erweima{absolute; top: left: border:1px solid #999; z-index:100;} .header .right #time{margin-left:20px;} .top{74px;1140px;10px auto} -------logo------- .logo{15px;} .logo a{block;} .slogo{170px;60px;10px; padding-left: border-left:1px solid #ccc;20px;} -------nav------- .nav{right;30px;30px;} .nav li{left; position:115px; .nav li a{font-size:20px;#3a3a3a;} .nav li:hover > a{#28a7e1} .nav li:hover > ul{visibility:visible; opacity:1; } .nav li a { -webkit-transition:color 0.2s linear,background 0.2s linear; -moz-transition: -o-transition: transition: .subnav {100;center;rgba(0,0.4); -webkit-transition:opacity 0.2s linear,visibility 0.2s linear; -moz-transition: -o-transition: transition: .subnav li{clear:100%;0 none;} .subnav li a{ .subnav li:hover > a{#80eef5;} -------footer----- .footer{#eee; padding-bottom:50px;} .footer a{#000;} .list1{20px 15px 40px;} .list1 li{40px;40px;} .list1 a.first:hover{url(img/redIcon.png) 0 6px no-repeat;} .list1 a.first{float:left;*/margin-right:23px; text-indent:15px; font-weight:bold;#111;} .sublist li{left;/**/11px;} .sublist li a{ .par{text-align: .par a{padding:0 10px;} .footer div.right{20px 25px 40px 0; } kefu #sidekf{url(../img/222.png) no-repeat left bottom;225px;200px;fixed; _position: right:-139px;top:_top:expression(documentElement.scrollTop+40);z-index:9;} #sidekf .lnk{ float:225px;} #sidekf .ovh{100px;} #sidekf a{128px;97px;} .sidekf_qq1{33px;35px;} .sidekf_qq2{36px;3px;} .sidekf_qq3{3px;} register.css .container{5px auto;1px solid #D8D8D8; #F9F9F9;50px;"微软雅黑"} .container h2{#333;} form{20px 0 0 120px;} form p{margin-bottom: form a{#ff7702;} h2{ label{48px;#F00; label.one{90px; input{#666;} .text,.submit{300px;46px; .error{ none;} .code{180px; _line-height: .get_code{110px; .agreement{12px;} .agreement input{12px;8px 8px 0 5px;url(../img/checkbox.png) no-repeat; }#337AB7;#FFF;} .submit{#F93; letter-spacing:4px; p.last{310px;16px;} p.last input{pointer;} check.php <?php parse_str($_POST['postData'],1)">$postif(!preg_match('/^1d{10}$/',1)">$post['telphone'])){ echo json_encode(['code'=>1,'sign'=>'telphone']); return ; } $post['code']!='123456'preg_match('/^.{6,16}$/',1)">$post['password'])){ $post['c_password']!=]){ preg_match('/^[x80-xff]{6,18}$/',1)">$post['name'preg_match('/^d{18}$/',1)">$post['idcard'preg_match('/^[da-z]+([._-]*[da-z]+)*@[da-z]+([.-][da-z]+)*.[a-z]+$/',1)">$post['email' ; } 将合法数据写入数据库 echo json_encode(['code'=>0]); UTF8编码下汉字Unicode编码范围:x80-xff //array (size=1) //'postData' => string 'telphone=222221&password=&c_password=&name=&idcard=&email=' (length=58) //array (size=6) // 'telphone' => string '222221' (length=6) // 'password' => string '' (length=0) // 'c_password' => string '' (length=0) // 'name' => string '' (length=0) // 'idcard' => string '' (length=0) // 'email' => string '' (length=0) 基础知识补充: 在线调试工具 “汉字”建议使用Unicode编码再进行匹配 不可见原子 自定义原子 通用字符类型作原子 元字符 ? ?边界限制符 量词 ? ?禁止贪婪 ?? 当该字符紧跟在任何一个其他限制符 (*,+,{n,},m}) u 模式修正符 模式单元 反向引用 定界符 模式修正符 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |