PHP 正则表达式匹配函数 preg_match 与 preg_match_all

PHP 正则表达式匹配函数 preg_match 与 preg_match_all

preg_match()

preg_match() 函数用于进行正则表达式匹配,成功返回 1 ,否则返回 0 。

语法:

1
int preg_match( string pattern, string subject [, 
array 
matches ] )

参数说明:

参数 说明
pattern 正则表达式
subject 需要匹配检索的对象
matches 可选,存储匹配结果的数组, $matches[0] 将包含与整个模式匹配的文本,$matches[1] 将包含与第一个捕获的括号中的子模式所匹配的文本,以此类推

例子 1:

1
2
3
4
5
6
7
8
9
<?php
if 
(preg_match(
"/php/i"

"PHP is the web scripting language of choice."

$matches
))
{
    
print 
"A match was found:" 

$matches
[0];
}
else
{
    
print 
"A match was not found."
;
}

输出:

1
A match was found:PHP

在该例子中,由于使用了 i 修正符,因此会不区分大小写去文本中匹配 php 。

 

注意:

preg_match() 第一次匹配成功后就会停止匹配,如果要实现全部结果的匹配,即搜索到subject结尾处,则需使用 preg_match_all() 函数。

例子 2 ,从一个 URL 中取得主机域名 :

1
2
3
4
5
6
7
8
<?php
// 从 URL 中取得主机名
preg_match(
"/^(http:\/\/)?([^\/]+)/i"
,
"http://blog.snsgou.com/index.php"

$matches
);
$host 

$matches
[2];
 
// 从主机名中取得后面两段
preg_match(
"/[^\.\/]+\.[^\.\/]+$/"

$host

$matches
);
echo 
"域名为:{$matches[0]}"
;

输出:

1
域名为:snsgou.com

 

preg_match_all()

preg_match_all() 函数用于进行正则表达式全局匹配,成功返回整个模式匹配的次数(可能为零),如果出错返回 FALSE 。

语法:

1
int preg_match_all( string pattern, string subject, 
array 
matches [, int flags ] )

参数说明:

参数 说明
pattern 正则表达式
subject 需要匹配检索的对象
matches 存储匹配结果的数组
flags

可选,指定匹配结果放入 matches 中的顺序,可供选择的标记有:

  1. PREG_PATTERN_ORDER:默认,对结果排序使 $matches[0] 为全部模式匹配的数组,$matches[1] 为第一个括号中的子模式所匹配的字符串组成的数组,以此类推
  2. PREG_SET_ORDER:对结果排序使 $matches[0] 为第一组匹配项的数组,$matches[1] 为第二组匹配项的数组,以此类推
  3. PREG_OFFSET_CAPTURE:如果设定本标记,对每个出现的匹配结果也同时返回其附属的字符串偏移量

下面的例子演示了将文本中所有 <pre></pre> 标签内的关键字(php)显示为红色。

1
2
3
4
5
6
7
8
9
10
11
12
<?php
$str 

"<pre>学习php是一件快乐的事。</pre><pre>所有的phper需要共同努力!</pre>"
;
$kw 

"php"
;
preg_match_all(
'/<pre>([\s\S]*?)<\/pre>/'

$str

$mat
);
for 
(
$i 
= 0; 
$i 

count
(
$mat
[0]); 
$i
++)
{
    
$mat
[0][
$i
] = 
$mat
[1][
$i
];
    
$mat
[0][
$i
] = 
str_replace
(
$kw

'<span style="color:#ff0000">' 

$kw 

'</span>'

$mat
[0][
$i
]);
    
$str 

str_replace
(
$mat
[1][
$i
], 
$mat
[0][
$i
], 
$str
);
}
echo 
$str
;
?>

输出效果:

PHP 正则表达式匹配函数 preg_match 与 preg_match_all

简化一下:

1
2
3
4
5
<?php
$str 

"<pre>学习php是一件快乐的事。</pre><pre>所有的phper需要共同努力!</pre>"
;
preg_match_all(
'/<pre>([\s\S]*?)<\/pre>/'

$str

$matches
);
print_r(
$matches
);
?>

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Array
(
    
[0] => Array
        
(
            
[0] => <pre>学习php是一件快乐的事。</pre>
            
[1] => <pre>所有的phper需要共同努力!</pre>
        
)
 
    
[1] => Array
        
(
            
[0] => 学习php是一件快乐的事。
            
[1] => 所有的phper需要共同努力!
        
)
 
)

 

 

参考:

http://php.net/manual/zh/function.preg-match-all.php

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/113731.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • heap和stack的区别

    heap和stack的区别heap和stack的区别

    2025年7月5日
    2
  • Redis集群搭建

    Redis集群搭建

    2021年6月15日
    112
  • docker导出镜像命令_docker save将容器保存为镜像

    docker导出镜像命令_docker save将容器保存为镜像导入导出命令介绍涉及的命令有export、import、save、loadsave示例dockersave-onginx.tarnginx:latest或dockersave>nginx.tarnginx:latest其中-o和>表示输出到文件,nginx.tar为目标文件,nginx:latest是源镜像名(name:tag),后面也可以是容器idload示例dockerload-inginx.tar或dockerload<n

    2022年9月6日
    6
  • Oracle集群(RAC)及 jdbc 连接双机数据库

    Oracle集群(RAC)及 jdbc 连接双机数据库

    2021年5月9日
    137
  • throw 和 throws 的区别?

    throw 和 throws 的区别?throw和throws的区别?throw:表示方法内抛出某种异常对象 如果异常对象是非RuntimeException则需要在方法申明时加上该异常的抛出即需要加上throws语句或者在方法体内trycatch处理该异常,否则编译报错 执行到throw语句则后面的语句块不再执行throws:方法的定义上使用throws表示这个方法可能抛出某种…

    2025年8月7日
    6
  • Java单例模式实现的两种方式和应用场景

    Java单例模式实现的两种方式和应用场景单例模式的定义个人理解,单例是指单个实例,在整个应用程序当中有且仅有一个实例存在,该实例是通过代码指定好的(自行创建的)。为什么要使用解决在高并发过程中,多个实例出现逻辑错误的情况。在特定的业务场景下避免对象重复创建,节约内存。实现的两种方式饿汉式顾名思义,不管有没有使用到该对象,只要程序启动成功,该单实例对象就存在。代码如下:/***饿汉式*/publicclassSingletonHungry{privatestaticSingletonHung

    2022年8月11日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号