preg_match_all

下面是PHP Manual中的内容

preg_match_all

 

 

preg_match_all — Perform a global regularexpression match

 

Report a bug

 Description

 

int preg_match_all ( string $pattern ,string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [,int $offset = 0 ]]] )

Searches subject for all matches to theregular expression given in pattern and puts them in matches in the orderspecified by flags.

 

After the first match is found, thesubsequent searches are continued on from end of the last match.

 

Report a bug

 Parameters

 

pattern

The pattern to search for, as a string.

 

subject

The input string.

 

matches

Array of all matches in multi-dimensionalarray ordered according to flags.

 

flags

Can be a combination of the following flags(note that it doesn't make sense to use PREG_PATTERN_ORDER together withPREG_SET_ORDER):

 

PREG_PATTERN_ORDER

Orders results so that $matches[0] is anarray of full pattern matches, $matches[1] is an array of strings matched bythe first parenthesized subpattern, and so on.

 

<?php

preg_match_all("|<[^>]+>(.*)</[^>]+>|U",

   "<b>example: </b><div align=left>this is atest</div>",

   $out, PREG_PATTERN_ORDER);

echo $out[0][0] . ", " .$out[0][1] . "\n";

echo $out[1][0] . ", " .$out[1][1] . "\n";

?>

The above example will output:

 

<b>example: </b>, <divalign=left>this is a test</div>

example: , this is a test

So, $out[0] contains array of strings thatmatched full pattern, and $out[1] contains array of strings enclosed by tags.

 

PREG_SET_ORDER

Orders results so that $matches[0] is anarray of first set of matches, $matches[1] is an array of second set ofmatches, and so on.

 

<?php

preg_match_all("|<[^>]+>(.*)</[^>]+>|U",

   "<b>example: </b><divalign=\"left\">this is a test</div>",

   $out, PREG_SET_ORDER);

echo $out[0][0] . ", " .$out[0][1] . "\n";

echo $out[1][0] . ", " .$out[1][1] . "\n";

?>

The above example will output:

 

<b>example: </b>, example:

<div align="left">this is atest</div>, this is a test

PREG_OFFSET_CAPTURE

If this flag is passed, for every occurringmatch the appendant string offset will also be returned. Note that this changesthe value of matches into an array where every element is an array consistingof the matched string at offset 0 and its string offset into subject at offset1.

 

If no order flag is given,PREG_PATTERN_ORDER is assumed.

 

offset

Normally, the search starts from thebeginning of the subject string. The optional parameter offset can be used tospecify the alternate place from which to start the search (in bytes).

 

Note:

 

Using offset is not equivalent to passingsubstr($subject, $offset) to preg_match_all() in place of the subject string,because pattern can contain assertions such as ^, $ or (?<=x). Seepreg_match() for examples.

 

Report a bug

 Return Values

 

Returns the number of full pattern matches(which might be zero), or FALSE if an error occurred.

我重点介绍一下/U的使用

1、使用/U

<?php

$userinfo = "Name: <b>ZeevSuraski</b> <br> Title: <b>PHP Guru</b>";

preg_match_all("/<b>(.*)<\/b>/U",$userinfo, $pat_array);

printf("%s <br /> %s",$pat_array[0][0],$pat_array[1][0]);

?>

匹配了两次

$pat_array[0][0] = <b>ZeevSuraski</b>

$pat_array[0][1] = <b>PHPGuru</b>

$pat_array[1][0] = Zeev Suraski

$pat_array[1][1] = PHP Guru

2、不使用/U

<?php

$userinfo = "Name: <b>ZeevSuraski</b> <br> Title: <b>PHP Guru</b>";

preg_match_all("/<b>(.*)<\/b>/",$userinfo, $pat_array);

printf("%s <br /> %s",$pat_array[0][0],$pat_array[1][0]);

?>

匹配了一次,贪婪匹配,匹配最外的<b>....</b>

$pat_array[0] =  <b>Zeev Suraski</b> <br>Title: <b>PHP Guru</b>

$pat_array[1] = Zeev Suraski</b><br> Title: <b>PHP Guru

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值