做者:极客小俊 一个专一于web技术的80后
我不用拼过聪明人,我只须要拼过那些懒人 我就必定会超越大部分人!
CSDN@极客小俊,原创文章, B站技术分享
B站视频 : 👉 Bilibili.com 👈
我的博客: 👉 cnblogs.com 👈php
咱们接着上一节的内容继续说正则..web
模式修正符 或者叫 模式单元
语法: /原子+元字符+量词/模式修正符号 或者说 /正则表达式/模式修正符
在php中模式修正符也叫修饰符,包含:i、m、s、U、e 模式修正符必须配合正则函数一块儿使用才有意义
例如: "/<img\ssrc=".?"/>/iU"正则表达式
1. 模式修正符就是几个字母 2. 能够一次使用一个,每个具必定的意义, 也能够连续使用多个 3. 是对整个正则表达式调优使用, 也能够说是对正则表达式功能的扩展 例如: "/abc/" 只能匹配小写字母 abc "/abc/i" 能够不区分大小写匹配 ABC abc Abc ABc AbC
模式修正符在php手册中查找以下位置:
经常使用模式修正符以下:
i : 表示在和模式进行匹配进不区分大小写、执行对大小写不敏感的匹配
m : 将字符串视为多行 视为多行后,任何一行均可以以什么开始或以什么结束 ^ 和 $
s : 将字符串视为单行、 若是没有使用这个模式修正符号时, 元字符中的"."默认不能匹配换行符号, 也就是说若是把一个字符串视为单行、那么这个换行符也就是一个普通符号了,不表明换行的意思了 ,因此 . 也就能够匹配上换行符了! [这里字符串是双引号哦]
x : 表示模式中的空白忽略不计,再说一次是正则模式中的空白 不是字符串中的!
e : 正则表达式必须使用在preg_replace替换字符串的函数中时才可使用 [如今已经废弃]
A : 必须以什么开头 通常都用^ [了解]
Z : 必须以什么结尾 通常都用$ [了解]
D: 必须以什么结尾 可是结尾字符串后必须是没得东西!设置m修正符后会失效!
U : 修正正则的贪婪模式
缘由: 正则表达式的特色:就是比较”贪婪“ .* .+ 全部字符都符合这个贪婪条件
修正贪婪以下方式:函数
模式修正符案例 以下:学习
$pattern='/abC/i'; $string='abcABC'; $pattern='/^w.+/im'; $string='abcABCcccc world element what wrong?'; $pattern='/^w.+/ims'; $string='abcABCcccc world element what wrong?'; $pattern='/this is php/ix'; $string='thisisphp'; $pattern='/this\s?is\s?php/i'; $string='this is php'; $pattern='/this is/AD'; $string='this is php'; $pattern='/^t.*/U'; $string='this is php'; $pattern='/^t.*?/U'; $string='this is php'; preg_match($pattern, $string,$arr); show($arr);
练习1: 匹配用户名必须是英文+数字 最长不超过8位, 最小5位 以下:this
$string='wang12345'; $pattern='/^[a-zA-Z0-9]{1}[a-zA-Z0-9]{4,7}/'; preg_match($pattern, $string,$arr); show($arr);
练习2:匹配Email 以下:url
$string='1562hejun466@qq.com'; //$string='mousesportsjonas@163.com'; //$string='mouses.ports.jonas@163.com'; //$string='mousical@public.net'; //$string='mousical@public'; $pattern='/^(?:\w+\.)?(?:\w+\.)?\w+@[a-zA-Z0-9]+(?:\.(?:net|org|com))?$/'; preg_match($pattern, $string,$arr); show($arr);
练习3:匹配一个HTTP URL的正则表达式 以下:spa
/* * 匹配URL 例如: * http://www.baidu.com * http://baidu.com * baidu.com * zhidao.baidu.com 等等 */ $string='http://www.baidu.com'; $string='https://baidu.com'; $string='https://www.baidu.com'; $string='baidu.com'; $string='zhidao.baidu.com'; $string='http://zhidao.baidu.com'; $pattern='/^(?:http[s]?:\/\/(?:www\.)?)?(?:[a-zA-Z0-9]+\.)?[a-zA-Z0-9]+\.(?:com|net|org)$/'; preg_match($pattern, $string,$arr); show($arr);
练习4:匹配手机号码与座机电话号码正则表达式 以下:.net
$string='63839154'; $string='023-63505008'; $string='18723188548'; $string='0371-60333332'; $pattern='/^1[35678]{1}\d{9}|0[0-9]{2,3}\-\d{7,8}|\d{7,8}/'; preg_match($pattern, $string,$arr); show($arr);
练习5 :匹配时光网的首页中的全部图片爬取出来 以下:ssr
$string=file_get_contents('http://www.mtime.com/'); $pattern='/<img\ssrc=\".*?\"\/>/'; preg_match_all($pattern, $string,$arrList); $patternReplace='/(?<=data-src=\").+?(?=\")/'; foreach ($arrList[0] as $k=>$v){ preg_match($patternReplace, $v,$arr); if(!empty($arr[0])){ $url[]=$arr[0]; $patternList[]='/(?<=src=\").+?(?=\")/'; echo preg_replace($patternList,$url,$arrList[0][$k]).'<br>'; } }
练习6:匹配将某个列表页面中的标题名称所有取出来循环成表格
//练习6:匹配将某个列表页面中的标题名称所有取出来循环成表格 $string=file_get_contents('http://www.cqepc.cn/'); $pattern='/\<div class=\"left\">.+?\<\/div\>/s'; preg_match_all($pattern, $string,$arr); echo '<h2 style="text-align:center;">重庆航天职大招生就业信息</h2>'; echo '<table border="1" cellpadding="0" cellspacing="0" width="700px" style="border-collapse:collapse; margin:20px auto;">'; foreach ($arr[0] as $k=>$v){ echo '<tr>'; echo '<td style="padding:5px;">'.$v.'</td>'; echo '</tr>'; } echo '</table>';
"点赞" "评论" "收藏"