<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >preg_match_all</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="Manual do PHP" HREF="index.html"><LINK REL="UP" TITLE="Regular Expression Functions (Perl-Compatible)" HREF="ref.pcre.html"><LINK REL="PREVIOUS" TITLE="preg_last_error" HREF="function.preg-last-error.html"><LINK REL="NEXT" TITLE="preg_match" HREF="function.preg-match.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></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" >Manual do PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.preg-last-error.html" ACCESSKEY="P" >Anterior</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" >Próxima</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="AEN166832" ></A ><P > (PHP 3 >= 3.0.9, PHP 4, PHP 5)</P >preg_match_all -- Perform a global regular expression match</DIV ><DIV CLASS="refsect1" ><A NAME="AEN166835" ></A ><H2 >Descrição</H2 >int <B CLASS="methodname" >preg_match_all</B > ( string pattern, string subject, array &matches [, int flags [, int offset]] )<BR ></BR ><P > Searches <CODE CLASS="parameter" >subject</CODE > for all matches to the regular expression given in <CODE CLASS="parameter" >pattern</CODE > and puts them in <CODE CLASS="parameter" >matches</CODE > in the order specified by <CODE CLASS="parameter" >flags</CODE >. </P ><P > After the first match is found, the subsequent searches are continued on from end of the last match. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN166861" ></A ><H2 >Parâmetros</H2 ><P > <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><CODE CLASS="parameter" >pattern</CODE ></DT ><DD ><P > The pattern to search for, as a string. </P ></DD ><DT ><CODE CLASS="parameter" >subject</CODE ></DT ><DD ><P > The input string. </P ></DD ><DT ><CODE CLASS="parameter" >matches</CODE ></DT ><DD ><P > In this case, $matches[0] is the first set of matches, and <CODE CLASS="varname" >$matches[0][0]</CODE > has text matched by full pattern, <CODE CLASS="varname" >$matches[0][1]</CODE > has text matched by first subpattern and so on. Similarly, <CODE CLASS="varname" >$matches[1]</CODE > is the second set of matches, etc. </P ></DD ><DT ><CODE CLASS="parameter" >flags</CODE ></DT ><DD ><P > 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 ><TT CLASS="constant" ><B >PREG_PATTERN_ORDER</B ></TT ></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. </P ><P > <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN166897" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />preg_match_all</font><font color="#007700">(</font><font color="#DD0000">"|<[^>]+>(.*)</[^>]+>|U"</font><font color="#007700">,<br /> </font><font color="#DD0000">"<b>example: </b><div align=left>this is a test</div>"</font><font color="#007700">,<br /> </font><font color="#0000BB">$out</font><font color="#007700">, </font><font color="#0000BB">PREG_PATTERN_ORDER</font><font color="#007700">);<br />echo </font><font color="#0000BB">$out</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">][</font><font color="#0000BB">0</font><font color="#007700">] . </font><font color="#DD0000">", " </font><font color="#007700">. </font><font color="#0000BB">$out</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">][</font><font color="#0000BB">1</font><font color="#007700">] . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />echo </font><font color="#0000BB">$out</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">][</font><font color="#0000BB">0</font><font color="#007700">] . </font><font color="#DD0000">", " </font><font color="#007700">. </font><font color="#0000BB">$out</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">][</font><font color="#0000BB">1</font><font color="#007700">] . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >O exemplo acima irá imprimir:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="html" ><b>example: </b>, <div align=left>this is a test</div> example: , this is a test</PRE ></TD ></TR ></TABLE ><P > 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 ><TT CLASS="constant" ><B >PREG_SET_ORDER</B ></TT ></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" ><P ></P ><A NAME="AEN166907" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />preg_match_all</font><font color="#007700">(</font><font color="#DD0000">"|<[^>]+>(.*)</[^>]+>|U"</font><font color="#007700">,<br /> </font><font color="#DD0000">"<b>example: </b><div align=\"left\">this is a test</div>"</font><font color="#007700">,<br /> </font><font color="#0000BB">$out</font><font color="#007700">, </font><font color="#0000BB">PREG_SET_ORDER</font><font color="#007700">);<br />echo </font><font color="#0000BB">$out</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">][</font><font color="#0000BB">0</font><font color="#007700">] . </font><font color="#DD0000">", " </font><font color="#007700">. </font><font color="#0000BB">$out</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">][</font><font color="#0000BB">1</font><font color="#007700">] . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />echo </font><font color="#0000BB">$out</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">][</font><font color="#0000BB">0</font><font color="#007700">] . </font><font color="#DD0000">", " </font><font color="#007700">. </font><font color="#0000BB">$out</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">][</font><font color="#0000BB">1</font><font color="#007700">] . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >O exemplo acima irá imprimir:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="html" ><b>example: </b>, example: <div align="left">this is a test</div>, this is a test</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DD ><DT ><TT CLASS="constant" ><B >PREG_OFFSET_CAPTURE</B ></TT ></DT ><DD ><P > If this flag is passed, for every occurring match the appendant string offset will also be returned. Note that this changes the value of <CODE CLASS="parameter" >matches</CODE > in an array where every element is an array consisting of the matched string at offset <TT CLASS="literal" >0</TT > and its string offset into <CODE CLASS="parameter" >subject</CODE > at offset <TT CLASS="literal" >1</TT >. </P ></DD ></DL ></DIV > </P ><P > If no order flag is given, <TT CLASS="constant" ><B >PREG_PATTERN_ORDER</B ></TT > is assumed. </P ></DD ><DT ><CODE CLASS="parameter" >offset</CODE ></DT ><DD ><P > Normally, the search starts from the beginning of the subject string. The optional parameter <CODE CLASS="parameter" >offset</CODE > can be used to specify the alternate place from which to start the search. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Nota: </B > Using <CODE CLASS="parameter" >offset</CODE > is not equivalent to passing <TT CLASS="literal" >substr($subject, $offset)</TT > to <B CLASS="function" >preg_match_all()</B > in place of the subject string, because <CODE CLASS="parameter" >pattern</CODE > can contain assertions such as <SPAN CLASS="emphasis" ><I CLASS="emphasis" >^</I ></SPAN >, <SPAN CLASS="emphasis" ><I CLASS="emphasis" >$</I ></SPAN > or <SPAN CLASS="emphasis" ><I CLASS="emphasis" >(?<=x)</I ></SPAN >. See <A HREF="function.preg-match.html" ><B CLASS="function" >preg_match()</B ></A > for examples. </P ></BLOCKQUOTE ></DIV ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN166938" ></A ><H2 >Valores de retornado</H2 ><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 ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN166942" ></A ><H2 >Histórico</H2 ><P > <DIV CLASS="informaltable" ><P ></P ><A NAME="AEN166945" ></A ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><THEAD ><TR ><TH >Versão</TH ><TH >Descrição</TH ></TR ></THEAD ><TBODY ><TR ><TD >4.3.3</TD ><TD > The <CODE CLASS="parameter" >offset</CODE > parameter was added </TD ></TR ><TR ><TD >4.3.0</TD ><TD > The <TT CLASS="constant" ><B >PREG_OFFSET_CAPTURE</B ></TT > flag was added </TD ></TR ></TBODY ></TABLE ><P ></P ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN166960" ></A ><H2 >Exemplos</H2 ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN166963" ></A ><P ><B >Exemplo 1. Getting all phone numbers out of some text.</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />preg_match_all</font><font color="#007700">(</font><font color="#DD0000">"/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x"</font><font color="#007700">,<br /> </font><font color="#DD0000">"Call 555-1212 or 1-800-555-1212"</font><font color="#007700">, </font><font color="#0000BB">$phones</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></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="AEN166967" ></A ><P ><B >Exemplo 2. Find matching HTML tags (greedy)</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// The \\2 is an example of backreferencing. This tells pcre that<br />// it must match the second set of parentheses in the regular expression<br />// itself, which would be the ([\w]+) in this case. The extra backslash is<br />// required because the string is in double quotes.<br /></font><font color="#0000BB">$html </font><font color="#007700">= </font><font color="#DD0000">"<b>bold text</b><a href=howdy.html>click me</a>"</font><font color="#007700">;<br /><br /></font><font color="#0000BB">preg_match_all</font><font color="#007700">(</font><font color="#DD0000">"/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/"</font><font color="#007700">, </font><font color="#0000BB">$html</font><font color="#007700">, </font><font color="#0000BB">$matches</font><font color="#007700">, </font><font color="#0000BB">PREG_SET_ORDER</font><font color="#007700">);<br /><br />foreach (</font><font color="#0000BB">$matches </font><font color="#007700">as </font><font color="#0000BB">$val</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">"matched: " </font><font color="#007700">. </font><font color="#0000BB">$val</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">] . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /> echo </font><font color="#DD0000">"part 1: " </font><font color="#007700">. </font><font color="#0000BB">$val</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">] . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /> echo </font><font color="#DD0000">"part 2: " </font><font color="#007700">. </font><font color="#0000BB">$val</font><font color="#007700">[</font><font color="#0000BB">3</font><font color="#007700">] . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /> echo </font><font color="#DD0000">"part 3: " </font><font color="#007700">. </font><font color="#0000BB">$val</font><font color="#007700">[</font><font color="#0000BB">4</font><font color="#007700">] . </font><font color="#DD0000">"\n\n"</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >O exemplo acima irá imprimir:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="html" >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 ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN166972" ></A ><H2 >Veja também</H2 ><P > <P ></P ><TABLE BORDER="0" ><TBODY ><TR ><TD ><A HREF="function.preg-match.html" ><B CLASS="function" >preg_match()</B ></A ></TD ></TR ><TR ><TD ><A HREF="function.preg-replace.html" ><B CLASS="function" >preg_replace()</B ></A ></TD ></TR ><TR ><TD ><A HREF="function.preg-split.html" ><B CLASS="function" >preg_split()</B ></A ></TD ></TR ></TBODY ></TABLE ><P ></P > </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-last-error.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Principal</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.preg-match.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >preg_last_error</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.pcre.html" ACCESSKEY="U" >Acima</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >preg_match</TD ></TR ></TABLE ></DIV ></BODY ></HTML >