Source of: /manual/en/function.preg-match.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.pcre.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'function.preg-match.php',
1 => 'preg_match',
),
'up' =>
array (
0 => 'ref.pcre.php',
1 => 'PCRE Functions',
),
'prev' =>
array (
0 => 'function.preg-match-all.php',
1 => 'preg_match_all',
),
'next' =>
array (
0 => 'function.preg-quote.php',
1 => 'preg_quote',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="function.preg-match" class="refentry">
<div class="refnamediv">
<h1 class="refname">preg_match</h1>
<p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">preg_match</span> — <span class="dc-title">Perform a regular expression match</span></p>
</div>
<a name="function.preg-match.description"></a><div class="refsect1 description">
<h3 class="title">Description</h3>
<div class="methodsynopsis dc-description">
<span class="type">int</span> <span class="methodname"><b>preg_match</b></span>
( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$pattern</tt></span>
, <span class="methodparam"><span class="type">string</span> <tt class="parameter">$subject</tt></span>
[, <span class="methodparam"><span class="type">array</span> <tt class="parameter reference">&$matches</tt></span>
[, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$flags</tt></span>
[, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$offset</tt></span>
]]] )</div>
<p class="para rdfs-comment">
Searches <i><tt class="parameter">subject</tt></i>
for a match to the regular
expression given in <i><tt class="parameter">pattern</tt></i>
.
</p>
</div>
<a name="function.preg-match.parameters"></a><div class="refsect1 parameters">
<h3 class="title">Parameters</h3>
<p class="para">
</p><dl>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">pattern</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
The pattern to search for, as a string.
</p>
</dd>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">subject</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
The input string.
</p>
</dd>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">matches</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
If <i><tt class="parameter">matches</tt></i>
is provided, then it is filled with
the results of search. <var class="varname">$matches[0]</var> will contain the
text that matched the full pattern, <var class="varname">$matches[1]</var>
will have the text that matched the first captured parenthesized
subpattern, and so on.
</p>
</dd>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">flags</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
<i><tt class="parameter">flags</tt></i>
can be the following flag:
</p><dl>
<dt class="varlistentry">
<span class="term"><b><tt class="constant">PREG_OFFSET_CAPTURE</tt></b></span>
</dt><dd class="listitem">
<span class="simpara">
If this flag is passed, for every occurring match the appendant string
offset will also be returned. Note that this changes the value of
<i><tt class="parameter">matches</tt></i>
into an array where every element is an
array consisting of the matched string at offset <i>0</i>
and its string offset into <i><tt class="parameter">subject</tt></i>
at offset
<i>1</i>.
</span>
</dd>
</dl>
<p>
</p>
</dd>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">offset</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
Normally, the search starts from the beginning of the subject string.
The optional parameter <i><tt class="parameter">offset</tt></i>
can be used to
specify the alternate place from which to start the search (in bytes).
</p>
<blockquote><p><b class="note">Note</b>:
Using <i><tt class="parameter">offset</tt></i>
is not equivalent to passing
<i>substr($subject, $offset)</i> to
<b>preg_match()</b> in place of the subject string,
because <i><tt class="parameter">pattern</tt></i>
can contain assertions such as
<em class="emphasis">^</em>, <em class="emphasis">$</em> or
<em class="emphasis">(?<=x)</em>. Compare:
</p><div class="informalexample">
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br />$subject </span><span style="color: #007700">= </span><span style="color: #DD0000">"abcdef"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$pattern </span><span style="color: #007700">= </span><span style="color: #DD0000">'/^def/'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #0000BB">$pattern</span><span style="color: #007700">, </span><span style="color: #0000BB">$subject</span><span style="color: #007700">, </span><span style="color: #0000BB">$matches</span><span style="color: #007700">, </span><span style="color: #0000BB">PREG_OFFSET_CAPTURE</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$matches</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<p class="para">The above example will output:</p>
<div class="example-contents screen">
<div class="cdata"><pre>
Array
(
)
</pre></div>
</div>
<p class="para">
while this example
</p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br />$subject </span><span style="color: #007700">= </span><span style="color: #DD0000">"abcdef"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$pattern </span><span style="color: #007700">= </span><span style="color: #DD0000">'/^def/'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #0000BB">$pattern</span><span style="color: #007700">, </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$subject</span><span style="color: #007700">,</span><span style="color: #0000BB">3</span><span style="color: #007700">), </span><span style="color: #0000BB">$matches</span><span style="color: #007700">, </span><span style="color: #0000BB">PREG_OFFSET_CAPTURE</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$matches</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<p class="para">
will produce
</p>
<div class="example-contents screen">
<div class="cdata"><pre>
Array
(
[0] => Array
(
[0] => def
[1] => 0
)
)
</pre></div>
</div>
</div><p>
<br />
</p></blockquote>
</dd>
</dl>
<p>
</p>
</div>
<a name="function.preg-match.returnvalues"></a><div class="refsect1 returnvalues">
<h3 class="title">Return Values</h3>
<p class="para">
<b>preg_match()</b> returns the number of times
<i><tt class="parameter">pattern</tt></i>
matches. That will be either 0 times
(no match) or 1 time because <b>preg_match()</b> will stop
searching after the first match. <a href="function.preg-match-all.php" class="function">preg_match_all()</a>
on the contrary will continue until it reaches the end of
<i><tt class="parameter">subject</tt></i>
.
<b>preg_match()</b> returns <b><tt class="constant">FALSE</tt></b> if an error occurred.
</p>
</div>
<a name="function.preg-match.changelog"></a><div class="refsect1 changelog">
<h3 class="title">Changelog</h3>
<p class="para">
</p><table class="doctable informaltable">
<thead valign="middle">
<tr valign="middle">
<th>Version</th>
<th>Description</th>
</tr>
</thead>
<tbody valign="middle" class="tbody">
<tr valign="middle">
<td align="left">5.2.2</td>
<td align="left">
Named subpatterns now accept the
syntax <i>(?<name>)</i>
and <i>(?'name')</i> as well
as <i>(?P<name>)</i>. Previous versions
accepted only <i>(?P<name>)</i>.
</td>
</tr>
<tr valign="middle">
<td align="left">4.3.3</td>
<td align="left">
The <i><tt class="parameter">offset</tt></i>
parameter was added
</td>
</tr>
<tr valign="middle">
<td align="left">4.3.0</td>
<td align="left">
The <b><tt class="constant">PREG_OFFSET_CAPTURE</tt></b> flag was added
</td>
</tr>
<tr valign="middle">
<td align="left">4.3.0</td>
<td align="left">
The <i><tt class="parameter">flags</tt></i>
parameter was added
</td>
</tr>
</tbody>
</table>
<p>
</p>
</div>
<a name="function.preg-match.examples"></a><div class="refsect1 examples">
<h3 class="title">Examples</h3>
<p class="para">
</p><div class="example">
<p><b>Example #1 Find the string of text "php"</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// The "i" after the pattern delimiter indicates a case-insensitive search<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #DD0000">"/php/i"</span><span style="color: #007700">, </span><span style="color: #DD0000">"PHP is the web scripting language of choice."</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"A match was found."</span><span style="color: #007700">;<br />} else {<br /> echo </span><span style="color: #DD0000">"A match was not found."</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
<p class="para">
</p><div class="example">
<p><b>Example #2 Find the word "web"</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="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 /></span><span style="color: #007700">if (</span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #DD0000">"/\bweb\b/i"</span><span style="color: #007700">, </span><span style="color: #DD0000">"PHP is the web scripting language of choice."</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"A match was found."</span><span style="color: #007700">;<br />} else {<br /> echo </span><span style="color: #DD0000">"A match was not found."</span><span style="color: #007700">;<br />}<br /><br />if (</span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #DD0000">"/\bweb\b/i"</span><span style="color: #007700">, </span><span style="color: #DD0000">"PHP is the website scripting language of choice."</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"A match was found."</span><span style="color: #007700">;<br />} else {<br /> echo </span><span style="color: #DD0000">"A match was not found."</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
<p class="para">
</p><div class="example">
<p><b>Example #3 Getting the domain name out of a URL</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// get host name from URL<br /></span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #DD0000">'@^(?:http://)?([^/]+)@i'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">"http://www.php.net/index.html"</span><span style="color: #007700">, </span><span style="color: #0000BB">$matches</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$host </span><span style="color: #007700">= </span><span style="color: #0000BB">$matches</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">];<br /><br /></span><span style="color: #FF8000">// get last two segments of host name<br /></span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #DD0000">'/[^.]+\.[^.]+$/'</span><span style="color: #007700">, </span><span style="color: #0000BB">$host</span><span style="color: #007700">, </span><span style="color: #0000BB">$matches</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"domain name is: </span><span style="color: #007700">{</span><span style="color: #0000BB">$matches</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents para"><p>The above example will output:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
domain name is: php.net
</pre></div>
</div>
</div><p>
</p>
<p class="para">
</p><div class="example">
<p><b>Example #4 Using named subpattern</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /><br />$str </span><span style="color: #007700">= </span><span style="color: #DD0000">'foobar: 2008'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Works in PHP 5.2.2 and later.<br /></span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #DD0000">'/(?<name>\w+): (?<digit>\d+)/'</span><span style="color: #007700">, </span><span style="color: #0000BB">$str</span><span style="color: #007700">, </span><span style="color: #0000BB">$matches</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Before PHP 5.2.2, use this:<br />// preg_match('/(?P<name>\w+): (?P<digit>\d+)/', $str, $matches);<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$matches</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents para"><p>The above example will output:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
Array
(
[0] => foobar: 2008
[name] => foobar
[1] => foobar
[digit] => 2008
[2] => 2008
)
</pre></div>
</div>
</div><p>
</p>
</div>
<a name="function.preg-match.notes"></a><div class="refsect1 notes">
<h3 class="title">Notes</h3>
<div class="tip"><b class="tip">Tip</b>
<p class="para">
Do not use <b>preg_match()</b> if you only want to check if
one string is contained in another string. Use
<a href="function.strpos.php" class="function">strpos()</a> or <a href="function.strstr.php" class="function">strstr()</a> instead as
they will be faster.
</p>
</div>
</div>
<a name="function.preg-match.seealso"></a><div class="refsect1 seealso">
<h3 class="title">See Also</h3>
<p class="para">
</p><ul class="simplelist">
<li class="member"><a href="function.preg-match-all.php" class="function" rel="rdfs-seeAlso">preg_match_all()</a> - Perform a global regular expression match</li>
<li class="member"><a href="function.preg-replace.php" class="function" rel="rdfs-seeAlso">preg_replace()</a> - Perform a regular expression search and replace</li>
<li class="member"><a href="function.preg-split.php" class="function" rel="rdfs-seeAlso">preg_split()</a> - Split string by a regular expression</li>
</ul><p>
</p>
</div>
</div><?php manual_footer(); ?>