一聚教程网:一个值得你收藏的教程网站

热门教程

preg_match 与 preg_match_all区别和详细用法

时间:2022-06-25 02:43:37 编辑:袖梨 来源:一聚教程网

正则表达式在 PHP 中的应用在 PHP 应用中,正则表达式主要用于:

正则匹配:根据正则表达式匹配相应的内容
正则替换:根据正则表达式匹配内容并替换
正则分割:根据正则表达式分割字符串
在 PHP 中有两类正则表达式函数,一类是 Perl 兼容正则表达式函数,一类是 POSIX 扩展正则表达式函数。二者差别不大,而且推荐使用Perl 兼容正则表达式函数,因此

下文都是以 Perl 兼容正则表达式函数为例子说明。

定界符Perl 兼容模式的正则表达式函数,其正则表达式需要写在定界符中。任何不是字母、数字或反斜线()的字符都可以作为定界符,通常我们使用 / 作为定界符。具体

使用见下面的例子。

提示
尽管正则表达式功能非常强大,但如果用普通字符串处理函数能完成的,就尽量不要用正则表达式函数,因为正则表达式效率会低得多。关于普通字符串处理函数,请参见《

PHP 字符串处理》。

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

语法:

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

例子 1 :

 代码如下 复制代码

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:

example: ,

this is a test

example: , this is a test

 代码如下 复制代码


preg_match_all("|<[^>]+>(.*)]+>|u",
    "example:

this is a test
",
    $out, preg_set_order);
echo $out[0][0] . ", " . $out[0][1] . "n";
echo $out[1][0] . ", " . $out[1][1] . "n";

 

int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags [, int $offset ]]] )
搜索主题的经常表达的方式给予配合

 

 代码如下 复制代码

$subject = "abcdefwww.111com.net";
$pattern = '/^def/';
preg_match($pattern, substr($subject,3), $matches, preg_offset_capture);
print_r($matches);


array
(
    [0] => array
        (
            [0] => def
            [1] => 0
        )

)

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

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

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

 代码如下 复制代码

// 从 URL 中取得主机名
preg_match("/^(http://)?([^/]+)/i",http://www.111com.net, $matches);
$host = $matches[2];
// 从主机名中取得后面两段
preg_match("/[^./]+.[^./]+$/", $host, $matches);
echo "域名为:{$matches[0]}";
?>


浏览器输出:

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

语法:

int preg_match_all( string pattern, string subject, array matches [, int flags ] )参数说明: 参数 说明
pattern 正则表达式
subject 需要匹配检索的对象
matches 存储匹配结果的数组
flags 可选,指定匹配结果放入 matches 中的顺序,可供选择的标记有:

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

下面的例子演示了将文本中所有

 标签内的关键字(php)显示为红色。

 代码如下 复制代码
$str = "
学习php是一件快乐的事。
所有的phper需要共同努力!
";
$kw = "php";
preg_match_all('/
([sS]*?)
/',$str,$mat);
for($i=0;$i     $mat[0][$i] = $mat[1][$i];
    $mat[0][$i] = str_replace($kw, ''.$kw.'', $mat[0][$i]);
    $str = str_replace($mat[1][$i], $mat[0][$i], $str);
}
echo $str;
?>

正则匹配中文汉字正则匹配中文汉字根据页面编码不同而略有区别:

GBK/GB2312编码:[x80-xff>]+ 或 [xa1-xff]+
UTF-8编码:[x{4e00}-x{9fa5}]+/u
例子:

 代码如下 复制代码

$str = "学习php是一件快乐的事。";
preg_match_all("/[x80-xff]+/", $str, $match);
//UTF-8 使用:
//preg_match_all("/[x{4e00}-x{9fa5}]+/u", $str, $match);
print_r($match);
?>输出:

Array
(
    [0] => Array
        (
            [0] => 学习
            [1] => 是一件快乐的事。
        )

)

热门栏目