Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 5414

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Perform a global regular expression match</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.preg-last-error.html">preg_last_error</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.preg-match.html">preg_match</a></div>
 <div class="up"><a href="ref.pcre.html">PCRE Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.preg-match-all" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">preg_match_all</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">preg_match_all</span> &mdash; <span class="dc-title">Perform a global regular expression match</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.preg-match-all-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">int</span> <span class="methodname"><strong>preg_match_all</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$pattern</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$subject</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$matches</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = <strong><code>PREG_PATTERN_ORDER</code></strong></span></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$offset</code><span class="initializer"> = 0</span></span>
  ]]] )</div>

  <p class="para rdfs-comment">
   Searches <em><code class="parameter">subject</code></em> for all matches to the regular
   expression given in <em><code class="parameter">pattern</code></em> and puts them in
   <em><code class="parameter">matches</code></em> in the order specified by
   <em><code class="parameter">flags</code></em>.
  </p>
  <p class="para">
   After the first match is found, the subsequent searches  are continued
   on from end of the last match.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.preg-match-all-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>

    <dt>

     <span class="term"><em><code class="parameter">pattern</code></em></span>
     <dd>

      <p class="para">
       The pattern to search for, as a string.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">subject</code></em></span>
     <dd>

      <p class="para">
       The input string.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">matches</code></em></span>
     <dd>

      <p class="para">
       Array of all matches in multi-dimensional array ordered according to
       <em><code class="parameter">flags</code></em>.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">flags</code></em></span>
     <dd>

      <p class="para">
       Can be a combination of the following flags (note that it doesn&#039;t make
       sense to use <strong><code>PREG_PATTERN_ORDER</code></strong> together with
       <strong><code>PREG_SET_ORDER</code></strong>):
       <dl>

        <dt>

         <span class="term"><strong><code>PREG_PATTERN_ORDER</code></strong></span>
         <dd>

          <p class="para">
           Orders results so that <var class="varname"><var class="varname">$matches[0]</var></var> is an array of full
           pattern matches, <var class="varname"><var class="varname">$matches[1]</var></var> is an array of strings matched by
           the first parenthesized subpattern, and so on.
          </p>
          <p class="para">
           <div class="informalexample">
            <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />preg_match_all</span><span style="color: #007700">(</span><span style="color: #DD0000">"|&lt;[^&gt;]+&gt;(.*)&lt;/[^&gt;]+&gt;|U"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"&lt;b&gt;example:&nbsp;&lt;/b&gt;&lt;div&nbsp;align=left&gt;this&nbsp;is&nbsp;a&nbsp;test&lt;/div&gt;"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PREG_PATTERN_ORDER</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">",&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">][</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">",&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">][</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</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="cdata"><pre>
&lt;b&gt;example: &lt;/b&gt;, &lt;div align=left&gt;this is a test&lt;/div&gt;
example: , this is a test
</pre></div>
            </div>
            <p class="para">
             So, <var class="varname"><var class="varname">$out[0]</var></var> contains array of strings that matched full pattern,
             and <var class="varname"><var class="varname">$out[1]</var></var> contains array of strings enclosed by tags.
            </p>
           </div>
          </p>
         </dd>

        </dt>

        <dt>

         <span class="term"><strong><code>PREG_SET_ORDER</code></strong></span>
         <dd>

          <p class="para">
           Orders results so that <var class="varname"><var class="varname">$matches[0]</var></var> is an array of first set
           of matches, <var class="varname"><var class="varname">$matches[1]</var></var> is an array of second set of matches,
           and so on.
           <div class="informalexample">
            <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />preg_match_all</span><span style="color: #007700">(</span><span style="color: #DD0000">"|&lt;[^&gt;]+&gt;(.*)&lt;/[^&gt;]+&gt;|U"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"&lt;b&gt;example:&nbsp;&lt;/b&gt;&lt;div&nbsp;align=\"left\"&gt;this&nbsp;is&nbsp;a&nbsp;test&lt;/div&gt;"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PREG_SET_ORDER</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">",&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">][</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">",&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">][</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</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="cdata"><pre>
&lt;b&gt;example: &lt;/b&gt;, example:
&lt;div align=&quot;left&quot;&gt;this is a test&lt;/div&gt;, this is a test
</pre></div>
            </div>
           </div>
          </p>
         </dd>

        </dt>

        <dt>

         <span class="term"><strong><code>PREG_OFFSET_CAPTURE</code></strong></span>
         <dd>

          <p class="para">
           If this flag is passed, for every occurring match the appendant string
           offset will also be returned. Note that this changes the value of
           <em><code class="parameter">matches</code></em> into an array where every element is an
           array consisting of the matched string at offset <em>0</em>
           and its string offset into <em><code class="parameter">subject</code></em> at offset
           <em>1</em>.
          </p>
         </dd>

        </dt>

       </dl>

      </p>
      <p class="para">
       If no order flag is given, <strong><code>PREG_PATTERN_ORDER</code></strong> is
       assumed.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">offset</code></em></span>
     <dd>

      <p class="para">
       Normally, the search starts from the beginning of the subject string.
       The optional parameter <em><code class="parameter">offset</code></em> 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 <em><code class="parameter">offset</code></em> is not equivalent to passing
        <em>substr($subject, $offset)</em> to
         <span class="function"><strong>preg_match_all()</strong></span> in place of the subject string,
        because <em><code class="parameter">pattern</code></em> can contain assertions such as
        <em class="emphasis">^</em>, <em class="emphasis">$</em> or
        <em class="emphasis">(?&lt;=x)</em>. See  <span class="function"><a href="function.preg-match.html" class="function">preg_match()</a></span>
        for examples.
       </p>
      </p></blockquote>
     </dd>

    </dt>

   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.preg-match-all-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns the number of full pattern matches (which might be zero),
   or <strong><code>FALSE</code></strong> if an error occurred.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.preg-match-all-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>5.4.0</td>
       <td>
        The <em><code class="parameter">matches</code></em> parameter became optional.
       </td>
      </tr>

      <tr>
       <td>5.3.6</td>
       <td>
        Returns <strong><code>FALSE</code></strong> if <em><code class="parameter">offset</code></em>
        is higher than
        <em><code class="parameter">subject</code></em> length.
       </td>
      </tr>

      <tr>
       <td>5.2.2</td>
       <td>
        Named subpatterns now accept the
        syntax <em>(?&lt;name&gt;)</em>
        and <em>(?&#039;name&#039;)</em> as well
        as <em>(?P&lt;name&gt;)</em>. Previous versions
        accepted only <em>(?P&lt;name&gt;)</em>.
       </td>
      </tr>

      <tr>
       <td>4.3.3</td>
       <td>
        The <em><code class="parameter">offset</code></em> parameter was added
       </td>
      </tr>

      <tr>
       <td>4.3.0</td>
       <td>
        The <strong><code>PREG_OFFSET_CAPTURE</code></strong> flag was added
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.preg-match-all-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-4785">
    <p><strong>Example #1 Getting all phone numbers out of some text.</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />preg_match_all</span><span style="color: #007700">(</span><span style="color: #DD0000">"/\(?&nbsp;&nbsp;(\d{3})?&nbsp;&nbsp;\)?&nbsp;&nbsp;(?(1)&nbsp;&nbsp;[\-\s]&nbsp;)&nbsp;\d{3}-\d{4}/x"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"Call&nbsp;555-1212&nbsp;or&nbsp;1-800-555-1212"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$phones</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4786">
    <p><strong>Example #2 Find matching HTML tags (greedy)</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;The&nbsp;\\2&nbsp;is&nbsp;an&nbsp;example&nbsp;of&nbsp;backreferencing.&nbsp;This&nbsp;tells&nbsp;pcre&nbsp;that<br />//&nbsp;it&nbsp;must&nbsp;match&nbsp;the&nbsp;second&nbsp;set&nbsp;of&nbsp;parentheses&nbsp;in&nbsp;the&nbsp;regular&nbsp;expression<br />//&nbsp;itself,&nbsp;which&nbsp;would&nbsp;be&nbsp;the&nbsp;([\w]+)&nbsp;in&nbsp;this&nbsp;case.&nbsp;The&nbsp;extra&nbsp;backslash&nbsp;is<br />//&nbsp;required&nbsp;because&nbsp;the&nbsp;string&nbsp;is&nbsp;in&nbsp;double&nbsp;quotes.<br /></span><span style="color: #0000BB">$html&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"&lt;b&gt;bold&nbsp;text&lt;/b&gt;&lt;a&nbsp;href=howdy.html&gt;click&nbsp;me&lt;/a&gt;"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">preg_match_all</span><span style="color: #007700">(</span><span style="color: #DD0000">"/(&lt;([\w]+)[^&gt;]*&gt;)(.*?)(&lt;\/\\2&gt;)/"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$html</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$matches</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PREG_SET_ORDER</span><span style="color: #007700">);<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$matches&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"matched:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"part&nbsp;1:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"part&nbsp;2:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"part&nbsp;3:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">[</span><span style="color: #0000BB">3</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"part&nbsp;4:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">[</span><span style="color: #0000BB">4</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n\n"</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="cdata"><pre>
matched: &lt;b&gt;bold text&lt;/b&gt;
part 1: &lt;b&gt;
part 2: b
part 3: bold text
part 4: &lt;/b&gt;

matched: &lt;a href=howdy.html&gt;click me&lt;/a&gt;
part 1: &lt;a href=howdy.html&gt;
part 2: a
part 3: click me
part 4: &lt;/a&gt;
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4787">
    <p><strong>Example #3 Using named subpattern</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$str&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;FOO<br /></span><span style="color: #DD0000">a:&nbsp;1<br />b:&nbsp;2<br />c:&nbsp;3<br /></span><span style="color: #007700">FOO;<br /><br /></span><span style="color: #0000BB">preg_match_all</span><span style="color: #007700">(</span><span style="color: #DD0000">'/(?P&lt;name&gt;\w+):&nbsp;(?P&lt;digit&gt;\d+)/'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$str</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$matches</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;This&nbsp;also&nbsp;works&nbsp;in&nbsp;PHP&nbsp;5.2.2&nbsp;(PCRE&nbsp;7.0)&nbsp;and&nbsp;later,&nbsp;however&nbsp;<br />&nbsp;*&nbsp;the&nbsp;above&nbsp;form&nbsp;is&nbsp;recommended&nbsp;for&nbsp;backwards&nbsp;compatibility&nbsp;*/<br />//&nbsp;preg_match_all('/(?&lt;name&gt;\w+):&nbsp;(?&lt;digit&gt;\d+)/',&nbsp;$str,&nbsp;$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="cdata"><pre>
Array
(
    [0] =&gt; Array
        (
            [0] =&gt; a: 1
            [1] =&gt; b: 2
            [2] =&gt; c: 3
        )

    [name] =&gt; Array
        (
            [0] =&gt; a
            [1] =&gt; b
            [2] =&gt; c
        )

    [1] =&gt; Array
        (
            [0] =&gt; a
            [1] =&gt; b
            [2] =&gt; c
        )

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

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

)
</pre></div>
    </div>
   </div>
  </p>
 </div>


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


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.preg-last-error.html">preg_last_error</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.preg-match.html">preg_match</a></div>
 <div class="up"><a href="ref.pcre.html">PCRE Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>