码迷,mamicode.com
首页 > Web开发 > 详细

php之正则表达式

时间:2016-12-04 00:16:18      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:echo   pattern   失败   ati   error   邮件   sel   har   字符   

主要参考书目《php和MySQL Web开发》《php核心技术和最佳实践》,此节主要是对php正则表达式的学习总结。

一、什么是正则表达式

正则表达式是对字符串某些特征的描述

有PCRE风格(函数以preg_为前缀)、POSIX风格(函数以ereg_为前缀)

 

二、正则表达式的一些表达规范

  1. 表示字符集和类

  任何包含在‘[]’中的内容,都是一个被匹配字符所属的字符集。其中,字符集只匹配一个字符。

[abcde]at···········································匹配at前一个字符为abcde其中一个的字符串

[a-z]at················································匹配at前一个字符为a-z其中一个的字符串

[a-zA-Z]at··········································匹配at前一个字符为a-z其中一个的字符串,含大写

[^a-z]·················································匹配at前一个字符不在a-z字符集中的字符串

 

  2. 表示重复

  符号“*”表示该模式可以重复0次或多次,符号“+”表示该模式至少出现一次,符号“{}”内可以写重复次数(例子见子表达式)

  3. 子表达式

  括号“()”中的字符为子表达式,如:

(very )+large····································表示匹配字符串的要求是large之前至少有一个”very ”

(very ){1,2}large····························{1,2}表示计数,子表达式重复1-2次

  4. 定位到字符串的开头和结尾

  符号“^”表示锁定之后的字符串为被匹配字符串的开头;符号“$”表示锁定之前的字符串为被匹配字符串的结尾。

^bob···············································只匹配以bob开头的字符串

Com$··············································只匹配以Com结尾的字符串

^[a-z]$ ···········································只匹配a-z之间一个字符的字符串

  5. 分支

  符号“|”表示分支选择。

123bc|def|ghi································匹配123bc或def或ghi

  6. 匹配特殊字符

  当需要匹配特殊字符时,必须在特殊字符前加一个“\”,如果要匹配一个“\”则必须用两个反斜杠“\\”来表示

 

三、正则表达式的应用

  1. 在顾客的反馈中查找特定的名词

  详见下例:

  这是一个简单的前台页面

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3     <head>
 4         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5         <title>index</title>
 6     </head>
 7 
 8     <body>
 9         <form id="form1" name="form1" method="post" action="search.php">
10             <label>请输入正则表达式</label>
11             <input type="text" name="search" id="search" />
12             <input type="submit" name="submit" value="搜索" />
13         </form>
14     </body>
15 </html>

  这是后台处理页面代码:

 1 <?php
 2 header("Content-Type:text/html;charset=utf-8");
 3 
 4 $con = mysql_connect("localhost","root","root");
 5 //当连接数据库出错时执行
 6 if(!$con){
 7      exit(‘链接数据库失败!‘);
 8 }
 9 //当连接表serch出错时执行
10 if(!mysql_select_db(‘search‘,$con)){
11        exit(mysqli_errno().":".mysql_error());
12 }
13 mysql_select_db(‘search_table‘);
14 mysql_query("set names utf8");
15 
16 $search=$_POST["search"];
17 
18 $sql="SELECT search_table_content FROM search_table where search_table_content REGEXP BINARY ‘".$search."‘";
19 echo $sql."<br>";
20 $querry = mysql_query($sql);
21 
22 while($info=mysql_fetch_array($querry)){
23     echo $info[‘search_table_content‘];
24     echo "<br>";
25 }
26     
27 
28 ?>

  2. 验证程序中用户的电子邮件

  ^[a-zA-Z0-9_\-.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-.]+$

      [a-zA-Z0-9_\-.]+  表示”至少由一个字母、数字、下划线、连字符、点号或者这些字符组合为开始的字符串”,用于匹配邮箱@前的用户名

    [a-zA-Z0-9\-]+  表示@后的主机名

    \.  利用转义字符,对“.”进行转义

    [a-zA-Z0-9\-.]+  表示域名

    类似的还有手机号码的校验,注册用户名是否符合规范等等

 

四、正则表达式对字符串的操作

  1. 用正则表达式查找子字符串

Int ereg(string pattern, string search, array [matches]);

search:该函数要搜索的字符串

pattern:在该字符串中寻找与正则表达式相匹配的字符串

matches:存储符合条件的字符串的数组

eregi() 除了不区分大小写,其他功能与ereg()一样

PHP 5.3 ereg() 无法正常使用,提示“Function ereg() is deprecated Error”,因为PHP5.3不再推荐使用POSIX正则函数库

要用preg_match来代替ereg

详见下例

  这是前台页面的代码:

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <title>无标题文档</title>
 6 </head>
 7 
 8 <body>
 9 <form id="form1" name="form1" method="post" action="email.php">
10   <label for="email">输入你的email:</label>
11   <input type="text" name="email" id="email" />
12   <input type="submit" name="submit" id="submit" value="提交" />
13 </form>
14 </body>
15 </html>

  这是后台处理页的代码:

 1 <?php
 2 header("Content-Type:text/html;charset=utf-8");
 3 
 4 $email = $_POST[‘email‘];
 5 $pattern = ‘/^[a-zA-Z0-9_\-.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-.]+$/‘;
 6 if(!preg_match($pattern, $email)){
 7 ?>
 8     <script language="javascript">
 9         alert("请输入正确的邮箱!");window.location.href="index.html";
10     </script>
11 <?php
12 }
13 ?>

 

  2. 用正则表达式替换子字符串

string ereg_replace(string pattern, string replacement, string sesrch);

在字符串search中查找正则表达式pattern的字符串,并且用字符串replacement代替

  3.用正则表达式分割字符串

    array split(string pattern, string search[, int max])

php之正则表达式

标签:echo   pattern   失败   ati   error   邮件   sel   har   字符   

原文地址:http://www.cnblogs.com/sssonia/p/6129848.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!