$p = '/apple/';
$str = "apple banna";
if (preg_match($p, $str)) {
echo 'matched';
}/foo bar/ #^[^0-9]$# ~php~
/http:\/\//
$p = 'http://'; $p = '/'.preg_quote($p, '/').'/'; echo $p;
$str = "Http://www.imooc.com/";
if (preg_match('/http/i', $str)) {
echo '匹配成功';
}<span size="4px">\ 一般用于转义字符
^ 断言目标的开始位置(或在多行模式下是行首)
$ 断言目标的结束位置(或在多行模式下是行尾)
. 匹配除换行符外的任何字符(默认)
[ 开始字符类定义
] 结束字符类定义
| 开始一个可选分支
( 子组的开始标记
) 子组的结束标记
? 作为量词,表示 0 次或 1 次匹配。位于量词后面用于改变量词的贪婪特性。 (查阅量词)
* 量词,0 次或多次匹配
+ 量词,1 次或多次匹配
{ 自定义量词开始标记
} 自定义量词结束标记</span><span>//下面的\s匹配任意的空白符,包括空格,制表符,换行符。[^\s]代表非空白符。[^\s]+表示一次或多次匹配非空白符。
$p = '/^我[^\s]+(苹果|香蕉)$/';
$str = "我喜欢吃苹果";
if (preg_match($p, $str)) {
echo '匹配成功';
}</span><span size="4px">元字符具有两种使用场景,一种是可以在任何地方都能使用,另一种是只能在方括号内使用,在方括号内使用的有:
\ 转义字符
^ 仅在作为第一个字符(方括号内)时,表明字符类取反
- 标记字符范围
其中^在反括号外面,表示断言目标的开始位置,但在方括号内部则代表字符类取反,方括号内的减号-可以标记字符范围,例如0-9表示0到9之间的所有数字。</span><span>//下面的\w匹配字母或数字或下划线。
$p = '/[\w\.\-]+@[a-z0-9\-]+\.(com|cn)/';
$str = "我的邮箱是marchalex@163.com";
preg_match($p, $str, $match);
echo $match[0];</span>//下面的\d表示匹配数字 $p = '/\d+\-\d+/'; $str = "我的电话是010-12345678"; preg_match($p, $str, $match); echo $match[0]; //结果为:010-12345678
$p = '/\d?\-\d?/'; $str = "我的电话是010-12345678"; preg_match($p, $str, $match); echo $match[0]; //结果为:0-1
$p = '/\d{3}\-\d{8}/';
$str = "我的电话是010-12345678";
preg_match($p, $str, $match);
echo $match[0]; //结果为:010-12345678$p = '/name:([\w\s]+)/'; $str = "name:steven jobs"; preg_match($p, $str, $match); echo $match[1]; //结果为:steven jobs
$subject = "abcdef"; $pattern = '/def/'; preg_match($pattern, $subject, $matches); print_r($matches); //结果为:Array ( [0] => def )
$subject = "abcdef"; $pattern = '/a(.*?)d/'; preg_match($pattern, $subject, $matches); print_r($matches); //结果为:Array ( [0] => abcd [1] => bc )
$subject = "my email is spark@imooc.com"; $pattern = '/[\w\-]+@\w+\.\w+/'; preg_match($pattern, $subject, $matches); echo $matches[0];
$p = "|<[^>]+>(.*?)</[^>]+>|i"; $str = "<b>example: </b><div align=left>this is a test</div>"; preg_match_all($p, $str, $matches); print_r($matches);
$p = "/<tr><td>(.*?)<\/td>\s*<td>(.*?)<\/td>\s*<\/tr>/i"; $str = "<table> <tr><td>Alex</td><td>25</td></tr> <tr><td>John</td><td>26</td></tr> </table>"; preg_match_all($p, $str, $matches); print_r($matches);
<?php
$str = "<ul>
<li>item 1</li>
<li>item 2</li>
</ul>";
//实现正则匹配所有li中的数据
$p = "/<li>(.*)<\/li>/i";//解释下这个正则://后面的i表示不区分大小写,<li>(.*?)<\/li>表示li标签内的匹配的()内的值有多少,括号内的.表示所有单字符,*表示数量为0个或者多个。也就是li标签内有字符就显示出来
preg_match_all($p, $str, $matches);
print_r($matches[1]);$string = 'April 15, 2014';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '$3, ${1} $2';
echo preg_replace($pattern, $replacement, $string); //结果为:2014, April 15$$patterns = array ('/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/',
'/^\s*{(\w+)}\s*=/');
$replace = array ('\3/\4/\1\2', '$\1 =');//\3等效于$3,\4等效于$4,依次类推
echo preg_replace($patterns, $replace, '{startDate} = 1999-5-27'); //结果为:$startDate = 5/27/1999
//详细解释下结果:(19|20)表示取19或者20中任意一个数字,(\d{2})表示两个数字,(\d{1,2})表示1个或2个数字,(\d{1,2})表示1个或2个数字。^\s*{(\w+)\s*=}表示以任意空格开头的,并且包含在{}中的字符,并且以任意空格结尾的,最后有个=号的。$$str = 'one two';
$str = preg_replace('/\s+/', ' ', $str);
echo $str; // 结果改变为'one two'$str = '主要有以下几个文件:index.php, style.css, common.js'; //将目标字符串$str中的文件名替换后增加em标 $p = '/\w+\.\w+/i'; $str = preg_replace($p, '<em>$0</em>', $str); echo $str;
<?php
$user = array(
'name' => 'marchalex',
'email' => 'marchalex@163.com',
'mobile' => '13312345678'
);
//进行一般性验证
if (empty($user)) {
die('用户信息不能为空');
}
if (strlen($user['name']) < 6) {
die('用户名长度最少为6位');
}
//用户名必须为字母、数字与下划线
if (!preg_match('/^\w+$/i', $user['name'])) {
die('用户名不合法');
}
//验证邮箱格式是否正确
if (!preg_match('/^[\w\.]+@\w+\.\w+$/i', $user['email'])) {
die('邮箱不合法');
}
//手机号必须为11位数字,且为1开头
if (!preg_match('/^1\d{10}$/i', $user['mobile'])) {
die('手机号不合法');
}
echo '用户信息验证成功';以上就介绍了PHP学习笔记(五)正则表达式,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号