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

preg_match()

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

语法:

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

参数说明:

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

例子 1:

<?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.";
}

输出:

A match was found:PHP

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

注意:

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

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

<?php
// 从 URL 中取得主机名
preg_match("/^(http:\/\/)?([^\/]+)/i","http://blog.snsgou.com/index.php", $matches);
$host = $matches[2];

// 从主机名中取得后面两段
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
echo "域名为:{$matches[0]}";

输出:

域名为:snsgou.com

preg_match_all()

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

语法:

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)显示为红色。

<?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
$str = "<pre>学习php是一件快乐的事。</pre><pre>所有的phper需要共同努力!</pre>";
preg_match_all(‘/<pre>([\s\S]*?)<\/pre>/‘, $str, $matches);
print_r($matches);
?>

输出:

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

时间: 2024-08-04 20:50:56

PHP 正则表达式匹配函数 preg_match 与 preg_match_all的相关文章

PHP正则表达式中函数preg_match()与preg_match-all的简单使用

<?php $pattern='/[0-9]/';                                           //定义匹配的模式 $subject='weuyr3ui76as83s0ck9';                 //定义需匹配的对象 $m1=$m2=array();                                          //初始化两个空数组 $num1=preg_match($pattern, $subject, $m1);  

php正则表达式匹配函数

<?php function show($var=null){ if(empty($var))  { echo 'null'; }else if(is_array($var) || is_object($var)){ echo '<pre>'; print_r($var); echo '</pre>'; }else{ echo $var; } } $pattern='/[0-9]/'; $subject='asd78dfgfd1dfg2asdf8sadf6asdf9'; $m

php 函数preg_match、preg_match_all ,以及正则表达式规则

<?php $str = 'php is the best language phhhhp is'; $part = '/ph{1,}p/'; echo preg_match($part, $str,$ar1); var_dump($ar1); echo preg_match_all($part, $str,$ar); var_dump($ar); echo $ar[0][0]; 1 array 0 => string 'php' (length=3) 2 array 0 => arra

php中函数preg_match或preg_match_all 第三个参数$match的解释

理解自:http://www.cnblogs.com/vicenteforever/articles/1623137.html php手册中是这样解释的 matches 如果提供了参数matches,它将被填充为搜索结果. $matches[0]将包含完整模式匹配到的文本, $matches[1] 将包含第一个捕获子组匹配到的文本,以此类推 到底是什么意思呢? $matchs[0]很好理解,就是整个$pattern 匹配的文本, $matchs[1]包含第一个捕获子组匹配到的文本, 列子: $p

PHP 正则表达式常用函数使用小结

在PHP中有两套正则表达式函数库.一套是由PCRE(Perl Compatible Regular Expression)库提供的.PCRE库使用和Perl相同的语法规则实现了正则表达式的模式匹配,其使用以“preg_”为前缀命名的函数.另一套是由POSIX(Portable Operation System interface)扩展库提供的.POSIX扩展的正则表达式由POSIX 1003.2定义,一般使用以“ereg_”为前缀命名的函数. 两套函数库的功能相似,执行效率稍有不同.一般而言,实

oracle 正则表达式 匹配

oracle 正则表达式 在实际应用中,想排除带有中文的字段值: select h.froomnumber from t_broker_house h where REGEXP_LIKE(froomnumber,'^([a-z0-9A-Z]|-)*$') 字符串’^198[0-9]$’可以匹配‘1980-1989’,如果希望统计出公司那些员工是80年-89年入职的,就可以使用如下的SQL语句: select * from emp e where regexp_like(to_char( e.hi

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

--http://www.5idev.com/p-php_preg_match.shtml 正则表达式在 PHP 中的应用 在 PHP 应用中,正则表达式主要用于: 正则匹配:根据正则表达式匹配相应的内容 正则替换:根据正则表达式匹配内容并替换 正则分割:根据正则表达式分割字符串 在 PHP 中有两类正则表达式函数,一类是 Perl 兼容正则表达式函数,一类是 POSIX 扩展正则表达式函数.二者差别不大,而且推荐使用Perl 兼容正则表达式函数,因此下文都是以 Perl 兼容正则表达式函数为例

php小经验:解析preg_match与preg_match_all 函数

php小经验:解析preg_match与preg_match_all 函数 本篇文章是对php中的preg_match函数与preg_match_all函数进行了详细的分析介绍,需要的朋友参考下 正则表达式在 PHP 中的应用在 PHP 应用中,正则表达式主要用于:•正则匹配:根据正则表达式匹配相应的内容•正则替换:根据正则表达式匹配内容并替换•正则分割:根据正则表达式分割字符串在 PHP 中有两类正则表达式函数,一类是 Perl 兼容正则表达式函数,一类是 POSIX 扩展正则表达式函数.二者

【Python3 爬虫】09_正则表达式(re.math()、re.search()、re.sub()、全局匹配函数)

re.math()函数 从源字符串的起始位置匹配一个模式 语法:re.match(pattern, string, flag) 第一个参数代表对应的正则表达式,第二个参数代表对应的源字符,第三个参数是可选参数,代表对应的标志位,可以放模式修正符等信息 #-*- codingn:utf-8 -*- import re string = "ipythonajsoasaoso" pattern = ".python." result = re.match(pattern,