<HTML ><HEAD ><TITLE >preg_match</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_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=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-match-all.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-quote.html" ACCESSKEY="N" >Next</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="AEN81544" ></A ><P > (PHP 3>= 3.0.9, PHP 4 )</P >preg_match -- Perform a regular expression match</DIV ><DIV CLASS="refsect1" ><A NAME="AEN81547" ></A ><H2 >Description</H2 >int <B CLASS="methodname" >preg_match</B > ( string pattern, string subject [, array matches [, int flags]])<BR ></BR ><P > Searches <TT CLASS="parameter" ><I >subject</I ></TT > for a match to the regular expression given in <TT CLASS="parameter" ><I >pattern</I ></TT >. </P ><P > If <TT CLASS="parameter" ><I >matches</I ></TT > is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on. </P ><P > <TT CLASS="parameter" ><I >flags</I ></TT > can be the following flag: <P ></P ><DIV CLASS="variablelist" ><DL ><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 > The <TT CLASS="parameter" ><I >flags</I ></TT > parameter is available since <TT CLASS="literal" >PHP</TT > 4.3.0 . </P ><P > <B CLASS="function" >preg_match()</B > returns the number of times <TT CLASS="parameter" ><I >pattern</I ></TT > 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 <TT CLASS="parameter" ><I >subject</I ></TT >. <B CLASS="function" >preg_match()</B > returns <TT CLASS="constant" ><B >FALSE</B ></TT > if an error occured. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN81591" ></A ><P ><B >Example 1. Find the string of text "php"</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >// the "i" after the pattern delimiter indicates a case-insensitive search if (preg_match ("/php/i", "PHP is the web scripting language of choice.")) { print "A match was found."; } else { print "A match was not found."; }</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN81594" ></A ><P ><B >Example 2. find the word "web"</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >// the \b in the pattern indicates a word boundary, so only the distinct // word "web" is matched, and not a word partial like "webbing" or "cobweb" if (preg_match ("/\bweb\b/i", "PHP is the web scripting language of choice.")) { print "A match was found."; } else { print "A match was not found."; } if (preg_match ("/\bweb\b/i", "PHP is the website scripting language of choice.")) { print "A match was found."; } else { print "A match was not found."; }</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN81597" ></A ><P ><B >Example 3. Getting the domain name out of a URL</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >// get host name from URL preg_match("/^(http:\/\/)?([^\/]+)/i", "http://www.php.net/index.html", $matches); $host = $matches[2]; // get last two segments of host name preg_match("/[^\.\/]+\.[^\.\/]+$/",$host,$matches); echo "domain name is: ".$matches[0]."\n";</PRE ></TD ></TR ></TABLE ><P > This example will produce: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >domain name is: php.net</PRE ></TD ></TR ></TABLE > </P ></DIV ></TD ></TR ></TABLE > See also <A HREF="function.preg-match-all.html" ><B CLASS="function" >preg_match_all()</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-match-all.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-quote.html" ACCESSKEY="N" >Next</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" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >preg_quote</TD ></TR ></TABLE ></DIV ></BODY ></HTML >