<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >preg_match</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_match_all" HREF="function.preg-match-all.html"><LINK REL="NEXT" TITLE="preg_quote" HREF="function.preg-quote.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-match-all.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-quote.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.preg-match" ></A >preg_match</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN166983" ></A ><P > (PHP 3 >= 3.0.9, PHP 4, PHP 5)</P >preg_match -- Perform a regular expression match</DIV ><DIV CLASS="refsect1" ><A NAME="AEN166986" ></A ><H2 >Descrição</H2 >int <B CLASS="methodname" >preg_match</B > ( string pattern, string subject [, array &matches [, int flags [, int offset]]] )<BR ></BR ><P > Searches <CODE CLASS="parameter" >subject</CODE > for a match to the regular expression given in <CODE CLASS="parameter" >pattern</CODE >. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN167009" ></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 > If <CODE CLASS="parameter" >matches</CODE > is provided, then it is filled with the results of search. <CODE CLASS="varname" >$matches[0]</CODE > will contain the text that matched the full pattern, <CODE CLASS="varname" >$matches[1]</CODE > will have the text that matched the first captured parenthesized subpattern, and so on. </P ></DD ><DT ><CODE CLASS="parameter" >flags</CODE ></DT ><DD ><P > <CODE CLASS="parameter" >flags</CODE > can be the following flag: <P ></P ><DIV CLASS="variablelist" ><DL ><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 return value 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 ></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()</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 >. Compare: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN167061" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$subject </font><font color="#007700">= </font><font color="#DD0000">"abcdef"</font><font color="#007700">;<br /></font><font color="#0000BB">$pattern </font><font color="#007700">= </font><font color="#DD0000">'/^def/'</font><font color="#007700">;<br /></font><font color="#0000BB">preg_match</font><font color="#007700">(</font><font color="#0000BB">$pattern</font><font color="#007700">, </font><font color="#0000BB">$subject</font><font color="#007700">, </font><font color="#0000BB">$matches</font><font color="#007700">, </font><font color="#0000BB">PREG_OFFSET_CAPTURE</font><font color="#007700">, </font><font color="#0000BB">3</font><font color="#007700">);<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$matches</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="screen" >Array ( )</PRE ></TD ></TR ></TABLE ><P > while this example </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$subject </font><font color="#007700">= </font><font color="#DD0000">"abcdef"</font><font color="#007700">;<br /></font><font color="#0000BB">$pattern </font><font color="#007700">= </font><font color="#DD0000">'/^def/'</font><font color="#007700">;<br /></font><font color="#0000BB">preg_match</font><font color="#007700">(</font><font color="#0000BB">$pattern</font><font color="#007700">, </font><font color="#0000BB">substr</font><font color="#007700">(</font><font color="#0000BB">$subject</font><font color="#007700">,</font><font color="#0000BB">3</font><font color="#007700">), </font><font color="#0000BB">$matches</font><font color="#007700">, </font><font color="#0000BB">PREG_OFFSET_CAPTURE</font><font color="#007700">);<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$matches</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > will produce </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [0] => Array ( [0] => def [1] => 0 ) )</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></BLOCKQUOTE ></DIV ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN167069" ></A ><H2 >Valores de retornado</H2 ><P > <B CLASS="function" >preg_match()</B > returns the number of times <CODE CLASS="parameter" >pattern</CODE > matches. That will be either 0 times (no match) or 1 time because <B CLASS="function" >preg_match()</B > will stop searching after the first match. <A HREF="function.preg-match-all.html" ><B CLASS="function" >preg_match_all()</B ></A > on the contrary will continue until it reaches the end of <CODE CLASS="parameter" >subject</CODE >. <B CLASS="function" >preg_match()</B > returns <TT CLASS="constant" ><B >FALSE</B ></TT > if an error occurred. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN167079" ></A ><H2 >Histórico</H2 ><P > <DIV CLASS="informaltable" ><P ></P ><A NAME="AEN167082" ></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 ><TR ><TD >4.3.0</TD ><TD > The <CODE CLASS="parameter" >flags</CODE > parameter was added </TD ></TR ></TBODY ></TABLE ><P ></P ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN167101" ></A ><H2 >Exemplos</H2 ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN167104" ></A ><P ><B >Exemplo 1. Find the string of text "php"</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 "i" after the pattern delimiter indicates a case-insensitive search<br /></font><font color="#007700">if (</font><font color="#0000BB">preg_match</font><font color="#007700">(</font><font color="#DD0000">"/php/i"</font><font color="#007700">, </font><font color="#DD0000">"PHP is the web scripting language of choice."</font><font color="#007700">)) {<br /> echo </font><font color="#DD0000">"A match was found."</font><font color="#007700">;<br />} else {<br /> echo </font><font color="#DD0000">"A match was not found."</font><font color="#007700">;<br />}<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="AEN167108" ></A ><P ><B >Exemplo 2. Find the word "web"</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 \b in the pattern indicates a word boundary, so only the distinct<br /> * word "web" is matched, and not a word partial like "webbing" or "cobweb" */<br /></font><font color="#007700">if (</font><font color="#0000BB">preg_match</font><font color="#007700">(</font><font color="#DD0000">"/\bweb\b/i"</font><font color="#007700">, </font><font color="#DD0000">"PHP is the web scripting language of choice."</font><font color="#007700">)) {<br /> echo </font><font color="#DD0000">"A match was found."</font><font color="#007700">;<br />} else {<br /> echo </font><font color="#DD0000">"A match was not found."</font><font color="#007700">;<br />}<br /><br />if (</font><font color="#0000BB">preg_match</font><font color="#007700">(</font><font color="#DD0000">"/\bweb\b/i"</font><font color="#007700">, </font><font color="#DD0000">"PHP is the website scripting language of choice."</font><font color="#007700">)) {<br /> echo </font><font color="#DD0000">"A match was found."</font><font color="#007700">;<br />} else {<br /> echo </font><font color="#DD0000">"A match was not found."</font><font color="#007700">;<br />}<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="AEN167112" ></A ><P ><B >Exemplo 3. Getting the domain name out of a URL</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// get host name from URL<br /></font><font color="#0000BB">preg_match</font><font color="#007700">(</font><font color="#DD0000">'@^(?:http://)?([^/]+)@i'</font><font color="#007700">,<br /> </font><font color="#DD0000">"http://www.php.net/index.html"</font><font color="#007700">, </font><font color="#0000BB">$matches</font><font color="#007700">);<br /></font><font color="#0000BB">$host </font><font color="#007700">= </font><font color="#0000BB">$matches</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">];<br /><br /></font><font color="#FF8000">// get last two segments of host name<br /></font><font color="#0000BB">preg_match</font><font color="#007700">(</font><font color="#DD0000">'/[^.]+\.[^.]+$/'</font><font color="#007700">, </font><font color="#0000BB">$host</font><font color="#007700">, </font><font color="#0000BB">$matches</font><font color="#007700">);<br />echo </font><font color="#DD0000">"domain name is: </font><font color="#007700">{</font><font color="#DD0000">$matches</font><font color="#007700">[</font><font color="#DD0000">0</font><font color="#007700">]}\n</font><font color="#DD0000">"</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="screen" >domain name is: php.net</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN167117" ></A ><H2 >Notas</H2 ><DIV CLASS="tip" ><BLOCKQUOTE CLASS="tip" ><P ><B >Dica: </B > Do not use <B CLASS="function" >preg_match()</B > if you only want to check if one string is contained in another string. Use <A HREF="function.strpos.html" ><B CLASS="function" >strpos()</B ></A > or <A HREF="function.strstr.html" ><B CLASS="function" >strstr()</B ></A > instead as they will be faster. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN167124" ></A ><H2 >Veja também</H2 ><P > <P ></P ><TABLE BORDER="0" ><TBODY ><TR ><TD ><A HREF="function.preg-match-all.html" ><B CLASS="function" >preg_match_all()</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-match-all.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-quote.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >preg_match_all</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_quote</TD ></TR ></TABLE ></DIV ></BODY ></HTML >