<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = 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',
    2 => 'Perform a regular expression match',
  ),
  'up' => 
  array (
    0 => 'ref.pcre.php',
    1 => 'PCRE Functions',
  ),
  'prev' => 
  array (
    0 => 'function.preg-last-error-msg.php',
    1 => 'preg_last_error_msg',
  ),
  'next' => 
  array (
    0 => 'function.preg-match-all.php',
    1 => 'preg_match_all',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/pcre/functions/preg-match.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.preg-match" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">preg_match</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">preg_match</span> &mdash; <span class="dc-title">Perform a regular expression match</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.preg-match-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>preg_match</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$pattern</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$subject</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter reference">&$matches</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$offset</code><span class="initializer"> = 0</span></span><br>): <span class="type"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment">
   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 parameters" id="refsect1-function.preg-match-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">pattern</code></dt>
     <dd>
      <p class="para">
       The pattern to search for, as a string.
      </p>
     </dd>
    
    
     <dt><code class="parameter">subject</code></dt>
     <dd>
      <p class="para">
       The input string.
      </p>
     </dd>
    
    
     <dt><code class="parameter">matches</code></dt>
     <dd>
      <p class="para">
       If <code class="parameter">matches</code> 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><code class="parameter">flags</code></dt>
     <dd>
      <p class="para">
       <code class="parameter">flags</code> can be a combination of the following flags:
       <dl>
        
         <dt><strong><code><a href="pcre.constants.php#constant.preg-offset-capture">PREG_OFFSET_CAPTURE</a></code></strong></dt>
         <dd>
          <p class="para">
           If this flag is passed, for every occurring match the appendant string
           offset (in bytes) will also be returned. Note that this changes the value of
           <code class="parameter">matches</code> into an array where every element is an
           array consisting of the matched string at offset <code class="literal">0</code>
           and its string offset into <code class="parameter">subject</code> at offset
           <code class="literal">1</code>.
           <div class="informalexample">
            <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />preg_match</span><span style="color: #007700">(</span><span style="color: #DD0000">'/(foo)(bar)(baz)/'</span><span style="color: #007700">, </span><span style="color: #DD0000">'foobarbaz'</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">?&gt;</span></span></code></div>
            </div>

            <p class="para">The above example will output:</p>
            <div class="example-contents screen">
<div class="annotation-interactive parameterscode"><pre class="parameterscode">Array
(
    [0] =&gt; Array
        (
            [0] =&gt; foobarbaz
            [1] =&gt; 0
        )

    [1] =&gt; Array
        (
            [0] =&gt; foo
            [1] =&gt; 0
        )

    [2] =&gt; Array
        (
            [0] =&gt; bar
            [1] =&gt; 3
        )

    [3] =&gt; Array
        (
            [0] =&gt; baz
            [1] =&gt; 6
        )

)</pre>
</div>
            </div>
           </div>
          </p>
         </dd>
        
        
         <dt><strong><code><a href="pcre.constants.php#constant.preg-unmatched-as-null">PREG_UNMATCHED_AS_NULL</a></code></strong></dt>
         <dd>
          <p class="para">
           If this flag is passed, unmatched subpatterns are reported as <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>;
           otherwise they are reported as an empty <span class="type"><a href="language.types.string.php" class="type string">string</a></span>.
           <div class="informalexample">
            <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />preg_match</span><span style="color: #007700">(</span><span style="color: #DD0000">'/(a)(b)*(c)/'</span><span style="color: #007700">, </span><span style="color: #DD0000">'ac'</span><span style="color: #007700">, </span><span style="color: #0000BB">$matches</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$matches</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #DD0000">'/(a)(b)*(c)/'</span><span style="color: #007700">, </span><span style="color: #DD0000">'ac'</span><span style="color: #007700">, </span><span style="color: #0000BB">$matches</span><span style="color: #007700">, </span><span style="color: #0000BB">PREG_UNMATCHED_AS_NULL</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$matches</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
            </div>

            <p class="para">The above example will output:</p>
            <div class="example-contents screen">
<div class="annotation-interactive parameterscode"><pre class="parameterscode">array(4) {
  [0]=&gt;
  string(2) &quot;ac&quot;
  [1]=&gt;
  string(1) &quot;a&quot;
  [2]=&gt;
  string(0) &quot;&quot;
  [3]=&gt;
  string(1) &quot;c&quot;
}
array(4) {
  [0]=&gt;
  string(2) &quot;ac&quot;
  [1]=&gt;
  string(1) &quot;a&quot;
  [2]=&gt;
  NULL
  [3]=&gt;
  string(1) &quot;c&quot;
}</pre>
</div>
            </div>
           </div>
          </p>
         </dd>
        
       </dl>
      </p>
     </dd>
    
    
     <dt><code class="parameter">offset</code></dt>
     <dd>
      <p class="para">
       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 (in bytes).
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        Using <code class="parameter">offset</code> is not equivalent to passing
        <code class="code">substr($subject, $offset)</code> to
        <span class="function"><strong>preg_match()</strong></span> in place of the subject string,
        because <code class="parameter">pattern</code> can contain assertions such as
        <em>^</em>, <em>$</em> or
        <em>(?&lt;=x)</em>. Compare:
        <div class="informalexample">
         <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">?&gt;</span></span></code></div>
         </div>

         <p class="para">The above example will output:</p>
         <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
Array
(
)
</pre></div>
         </div>
         <p class="para">
          while this example
         </p>
         <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">?&gt;</span></span></code></div>
         </div>

         <p class="para">
          will produce
         </p>
         <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
Array
(
    [0] =&gt; Array
        (
            [0] =&gt; def
            [1] =&gt; 0
        )

)
</pre></div>
         </div>
         <p class="para">
          Alternatively, to avoid using <span class="function"><a href="function.substr.php" class="function">substr()</a></span>, use the
          <code class="literal">\G</code> assertion rather than the <code class="literal">^</code> anchor, or
          the <code class="literal">A</code> modifier instead, both of which work with
          the <code class="parameter">offset</code> parameter.
         </p>
        </div>
       </p>
      </p></blockquote>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.preg-match-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   <span class="function"><strong>preg_match()</strong></span> returns 1 if the <code class="parameter">pattern</code>
   matches given <code class="parameter">subject</code>, 0 if it does not, or <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> on failure.
  </p>
  <div class="warning"><strong class="warning">Warning</strong><p class="simpara">This function may
return Boolean <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>, but may also return a non-Boolean value which
evaluates to <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>. Please read the section on <a href="language.types.boolean.php" class="link">Booleans</a> for more
information. Use <a href="language.operators.comparison.php" class="link">the ===
operator</a> for testing the return value of this
function.</p></div>
 </div>


 <div class="refsect1 errors" id="refsect1-function.preg-match-errors">
  <h3 class="title">Errors/Exceptions</h3>
  <p class="para">
If the regex pattern passed does not compile to a valid regex, an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> is emitted.
</p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.preg-match-changelog">
  <h3 class="title">Changelog</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.2.0</td>
       <td>
        The <strong><code><a href="pcre.constants.php#constant.preg-unmatched-as-null">PREG_UNMATCHED_AS_NULL</a></code></strong> is now supported for the
        <code class="parameter">$flags</code> parameter.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.preg-match-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 Find the string of text &quot;php&quot;</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>Example #2 Find the word &quot;web&quot;</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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 />echo </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<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">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3">
    <p><strong>Example #3 Getting the domain name out of a URL</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">domain name is: php.net</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4">
    <p><strong>Example #4 Using named subpattern</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #DD0000">'/(?P&lt;name&gt;\w+): (?P&lt;digit&gt;\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">/* Alternative */<br />// preg_match('/(?&lt;name&gt;\w+): (?&lt;digit&gt;\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">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Array
(
    [0] =&gt; foobar: 2008
    [name] =&gt; foobar
    [1] =&gt; foobar
    [digit] =&gt; 2008
    [2] =&gt; 2008
)</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.preg-match-notes">
  <h3 class="title">Notes</h3>
  <div class="tip"><strong class="tip">Tip</strong>
   <p class="para">
    Do not use <span class="function"><strong>preg_match()</strong></span> if you only want to check if
    one string is contained in another string. Use
    <span class="function"><a href="function.strpos.php" class="function">strpos()</a></span> instead as it will be faster.
   </p>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.preg-match-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><a href="pcre.pattern.php" class="link">PCRE Patterns</a></li>
    <li><span class="function"><a href="function.preg-quote.php" class="function" rel="rdfs-seeAlso">preg_quote()</a> - Quote regular expression characters</span></li>
    <li><span class="function"><a href="function.preg-match-all.php" class="function" rel="rdfs-seeAlso">preg_match_all()</a> - Perform a global regular expression match</span></li>
    <li><span class="function"><a href="function.preg-replace.php" class="function" rel="rdfs-seeAlso">preg_replace()</a> - Perform a regular expression search and replace</span></li>
    <li><span class="function"><a href="function.preg-split.php" class="function" rel="rdfs-seeAlso">preg_split()</a> - Split string by a regular expression</span></li>
    <li><span class="function"><a href="function.preg-last-error.php" class="function" rel="rdfs-seeAlso">preg_last_error()</a> - Returns the error code of the last PCRE regex execution</span></li>
    <li><span class="function"><a href="function.preg-last-error-msg.php" class="function" rel="rdfs-seeAlso">preg_last_error_msg()</a> - Returns the error message of the last PCRE regex execution</span></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>