<HTML ><HEAD ><TITLE >preg_match_all</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Regular Expression Functions (Perl-Compatible)" HREF="ref.pcre.html"><LINK REL="PREVIOUS" TITLE="preg_grep" HREF="function.preg-grep.html"><LINK REL="NEXT" TITLE="preg_match" HREF="function.preg-match.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.preg-grep.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.preg-match.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.preg-match-all" ></A >preg_match_all</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN81471" ></A ><P > (PHP 3>= 3.0.9, PHP 4 )</P >preg_match_all -- Perform a global regular expression match</DIV ><DIV CLASS="refsect1" ><A NAME="AEN81474" ></A ><H2 >Description</H2 >int <B CLASS="methodname" >preg_match_all</B > ( string pattern, string subject, array matches [, int flags])<BR ></BR ><P > Searches <TT CLASS="parameter" ><I >subject</I ></TT > for all matches to the regular expression given in <TT CLASS="parameter" ><I >pattern</I ></TT > and puts them in <TT CLASS="parameter" ><I >matches</I ></TT > in the order specified by <TT CLASS="parameter" ><I >flags</I ></TT >. </P ><P > After the first match is found, the subsequent searches are continued on from end of the last match. </P ><P > <TT CLASS="parameter" ><I >flags</I ></TT > can be a combination of the following flags (note that it doesn't make sense to use <TT CLASS="constant" ><B >PREG_PATTERN_ORDER</B ></TT > together with <TT CLASS="constant" ><B >PREG_SET_ORDER</B ></TT >): <P ></P ><DIV CLASS="variablelist" ><DL ><DT >PREG_PATTERN_ORDER</DT ><DD ><P > Orders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on. <DIV CLASS="informalexample" ><A NAME="AEN81506" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php preg_match_all ("|<[^>]+>(.*)</[^>]+>|U", "<b>example: </b><div align=left>this is a test</div>", $out, PREG_PATTERN_ORDER); print $out[0][0].", ".$out[0][1]."\n"; print $out[1][0].", ".$out[1][1]."\n"; ?></PRE ></TD ></TR ></TABLE ><P > This example will produce: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" ><b>example: </b>, <div align=left>this is a test</div> example: , this is a test</PRE ></TD ></TR ></TABLE > So, $out[0] contains array of strings that matched full pattern, and $out[1] contains array of strings enclosed by tags. </P ><P ></P ></DIV > </P ></DD ><DT >PREG_SET_ORDER</DT ><DD ><P > Orders results so that $matches[0] is an array of first set of matches, $matches[1] is an array of second set of matches, and so on. <DIV CLASS="informalexample" ><A NAME="AEN81514" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php preg_match_all ("|<[^>]+>(.*)</[^>]+>|U", "<b>example: </b><div align=left>this is a test</div>", $out, PREG_SET_ORDER); print $out[0][0].", ".$out[0][1]."\n"; print $out[1][0].", ".$out[1][1]."\n"; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > This example will produce: <DIV CLASS="informalexample" ><A NAME="AEN81516" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><b>example: </b>, example: <div align=left>this is a test</div>, this is a test</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > In this case, $matches[0] is the first set of matches, and $matches[0][0] has text matched by full pattern, $matches[0][1] has text matched by first subpattern and so on. Similarly, $matches[1] is the second set of matches, etc. </P ></DD ><DT >PREG_OFFSET_CAPTURE</DT ><DD ><P > If this flag is set, for every occuring match the appendant string offset will also be returned. Note that this changes the return value in an array where every element is an array consisting of the matched string at offset <TT CLASS="literal" >0</TT > and it's string offset into <TT CLASS="parameter" ><I >subject</I ></TT > at offset <TT CLASS="literal" >1</TT >. This flag is available since <TT CLASS="literal" >PHP</TT > 4.3.0 . </P ></DD ></DL ></DIV > </P ><P > If no order flag is given, <TT CLASS="constant" ><B >PREG_PATTERN_ORDER</B ></TT > is assumed. </P ><P > Returns the number of full pattern matches (which might be zero), or <TT CLASS="constant" ><B >FALSE</B ></TT > if an error occurred. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN81531" ></A ><P ><B >Example 1. Getting all phone numbers out of some text.</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php preg_match_all ("/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x", "Call 555-1212 or 1-800-555-1212", $phones); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN81535" ></A ><P ><B >Example 2. Find matching HTML tags (greedy)</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // The \\2 is an example of backreferencing. This tells pcre that // it must match the second set of parentheses in the regular expression // itself, which would be the ([\w]+) in this case. The extra backslash is // required because the string is in double quotes. $html = "<b>bold text</b><a href=howdy.html>click me</a>"; preg_match_all ("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $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]."\n\n"; } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > This example will produce: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >matched: <b>bold text</b> part 1: <b> part 2: bold text part 3: </b> matched: <a href=howdy.html>click me</a> part 1: <a href=howdy.html> part 2: click me part 3: </a></PRE ></TD ></TR ></TABLE > </P ><P > See also <A HREF="function.preg-match.html" ><B CLASS="function" >preg_match()</B ></A >, <A HREF="function.preg-replace.html" ><B CLASS="function" >preg_replace()</B ></A >, and <A HREF="function.preg-split.html" ><B CLASS="function" >preg_split()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.preg-grep.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.preg-match.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >preg_grep</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.pcre.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >preg_match</TD ></TR ></TABLE ></DIV ></BODY ></HTML >