Home  >  Article  >  Backend Development  >  Introduction to preg_match_all related information_PHP tutorial

Introduction to preg_match_all related information_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:39:31927browse

 preg_match_all 进行全局正则表达式匹配,这篇文章主要介绍下preg_match_all的相关资料,需要的朋友可以参考下

preg_match_all — 进行全局正则表达式匹配   说明    代码如下: int preg_match_all ( string pattern, string subject, array matches [, int flags] )     在 subject 中搜索所有与 pattern 给出的正则表达式匹配的内容并将结果以 flags 指定的顺序放到 matches 中。 搜索到第一个匹配项之后,接下来的搜索从上一个匹配项末尾开始。   特别注意PREG_PATTERN_ORDER 和PREG_SET_ORDER   flags 可以是下列标记的组合(注意把 PREG_PATTERN_ORDER 和 PREG_SET_ORDER 合起来用没有意义):   如果使用PREG_PATTERN_ORDER   对结果排序使 $matches[0] 为全部模式匹配的数组,$matches[1] 为第一个括号中的子模式所匹配的字符串组成的数组,以此类推。(即$matches[0] [0]为全部模式匹配中的每一项,$matches[0] [1]为全部模式匹配中的第二项,$matches[1] [0]为匹配每一个括号中的第一项,$matches[1] [0]为匹配每一个括号中的第二项)   代码如下: ]+>(.*)]+>|U","example:
this is a test
",$out, PREG_PATTERN_ORDER);   print $out[0][0].", ".$out[0][1]."\n";  print $out[1][0].", ".$out[1][1]."\n";  ?>      本例将输出:  代码如下: example: ,
this is a test
  example: , this is a test     因此,$out[0] 包含匹配整个模式的字符串,$out[1] 包含一对 HTML 标记之间的字符串。 如果使用PREG_SET_ORDER   对结果排序使 $matches[0] 为第一组匹配项的数组,$matches[1] 为第二组匹配项的数组,以此类推。(即$matches[0] [0]为第一组匹配项中完整匹配的字符串,$matches[0] [1]为第一组匹配中完整匹配第一个括号中的字符串)    代码如下: ]+>(.*)]+>|U","example:
this is a test
",$out, PREG_SET_ORDER); print $out[0][0].", ".$out[0][1]."\n";  print $out[1][0].", ".$out[1][1]."\n";  ?>     本例将输出: 代码如下: example: , example: 
this is a test
, this is a test ​ ​ In this example, $matches[0] is the first set of matching results, $matches[0][0] contains the text matching the entire pattern, $matches[0][1] contains the text matching the first sub-pattern, ending with And so on. Likewise, $matches[1] is the second set of matches, and so on. ​ PREG_OFFSET_CAPTURE ​ If this flag is set, the associated string offset will also be returned for each occurrence of a match. Note that this changes the value of the returned array so that each cell in it is also an array, where the first item is the matched string and the second item is its offset within the subject. This tag is available since PHP 4.3.0. If no tag is given, PREG_PATTERN_ORDER is assumed. Returns the number of times the entire pattern was matched (possibly zero), or FALSE on error. ​ Example 1. Get all phone numbers from a text ​ The code is as follows: ​ ​ Example 2. Search for matching HTML tags (greedy) The code is as follows: bold textclick me"; preg_match_all ("/(<([w]+)[^>]*>)(.*)()/", $html, $matches); for ($i=0; $i< count($matches[0]); $i++) { echo "matched: ".$matches[0][$i]."n"; echo "part 1: ".$matches[1][$i]."n"; echo "part 2: ".$matches[3][$i]."n"; echo "part 3: ".$matches[4][$i]."nn"; } ?> ​ ​ This example will output: ​ The code is as follows: matched: bold text part 1: part 2: bold text part 3: matched: click me part 1: part 2: click me part 3:

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/730223.htmlTechArticlepreg_match_all performs global regular expression matching. This article mainly introduces the relevant information of preg_match_all. Friends who need it can refer to it. Next preg_match_all performs global regular expression matching...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn