Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 5417

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 regular expression search and replace using a callback</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.preg-quote.html">preg_quote</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.preg-replace.html">preg_replace</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-replace-callback" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">preg_replace_callback</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.5, PHP 5)</p><p class="refpurpose"><span class="refname">preg_replace_callback</span> &mdash; <span class="dc-title">Perform a regular expression search and replace using a callback</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.preg-replace-callback-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>preg_replace_callback</strong></span>
    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$pattern</code></span>
   , <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$subject</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$limit</code><span class="initializer"> = -1</span></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter reference">&$count</code></span>
  ]] )</div>

  <p class="para rdfs-comment">
   The behavior of this function is almost identical to
    <span class="function"><a href="function.preg-replace.html" class="function">preg_replace()</a></span>, except for the fact that instead of
   <em><code class="parameter">replacement</code></em> parameter, one should specify a
   <em><code class="parameter">callback</code></em>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.preg-replace-callback-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. It can be either a string or an array with
       strings.
      </p>
     </dd>

    </dt>

    <dt>

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

      <p class="para">
       A callback that will be called and passed an array of matched elements
       in the <em><code class="parameter">subject</code></em> string. The callback should
       return the replacement string. This is the callback signature:
      </p>
      <p class="para">
       <div class="methodsynopsis dc-description">
        <span class="type">string</span> <span class="methodname"><span class="replaceable">handler</span></span>
         ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$matches</code></span>
        )</div>

      </p>
      <p class="para">
       You&#039;ll often need the <em><code class="parameter">callback</code></em> function
       for a  <span class="function"><strong>preg_replace_callback()</strong></span> in just one place.
       In this case you can use an
       <a href="functions.anonymous.html" class="link">anonymous function</a> to
       declare the callback within the call to
        <span class="function"><strong>preg_replace_callback()</strong></span>. By doing it this way
       you have all information for the call in one place and do not
       clutter the function namespace with a callback function&#039;s name
       not used anywhere else.
      </p>
      <p class="para">
       <div class="example" id="example-4794">
        <p><strong>Example #1  <span class="function"><strong>preg_replace_callback()</strong></span> and 
        anonymous function</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;a&nbsp;unix-style&nbsp;command&nbsp;line&nbsp;filter&nbsp;to&nbsp;convert&nbsp;uppercase<br />&nbsp;*&nbsp;letters&nbsp;at&nbsp;the&nbsp;beginning&nbsp;of&nbsp;paragraphs&nbsp;to&nbsp;lowercase&nbsp;*/<br /></span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"php://stdin"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"r"</span><span style="color: #007700">)&nbsp;or&nbsp;die(</span><span style="color: #DD0000">"can't&nbsp;read&nbsp;stdin"</span><span style="color: #007700">);<br />while&nbsp;(!</span><span style="color: #0000BB">feof</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$line&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$line&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">preg_replace_callback</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'|&lt;p&gt;\s*\w|'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;(</span><span style="color: #0000BB">$matches</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">strtolower</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">]);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$line<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$line</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
        </div>

       </div>
      </p>
     </dd>

    </dt>

    <dt>

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

      <p class="para">
       The string or an array with strings to search and replace.
      </p>
     </dd>

    </dt>

    <dt>

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

      <p class="para">
       The maximum possible replacements for each pattern in each
       <em><code class="parameter">subject</code></em> string. Defaults to
       <em>-1</em> (no limit).
      </p>
     </dd>

    </dt>

    <dt>

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

      <p class="para">
       If specified, this variable will be filled with the number of
       replacements done.
      </p>
     </dd>

    </dt>

   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.preg-replace-callback-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
    <span class="function"><strong>preg_replace_callback()</strong></span> returns an array if the
   <em><code class="parameter">subject</code></em> parameter is an array, or a string
   otherwise. On errors the return value is <strong><code>NULL</code></strong>
  </p>
  <p class="para">
   If matches are found, the new subject will be returned, otherwise
   <em><code class="parameter">subject</code></em> will be returned unchanged. 
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.preg-replace-callback-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.1.0</td>
       <td>
        The <em><code class="parameter">count</code></em> parameter was added
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.preg-replace-callback-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-4795">
    <p><strong>Example #2  <span class="function"><strong>preg_replace_callback()</strong></span> example</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;this&nbsp;text&nbsp;was&nbsp;used&nbsp;in&nbsp;2002<br />//&nbsp;we&nbsp;want&nbsp;to&nbsp;get&nbsp;this&nbsp;up&nbsp;to&nbsp;date&nbsp;for&nbsp;2003<br /></span><span style="color: #0000BB">$text&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"April&nbsp;fools&nbsp;day&nbsp;is&nbsp;04/01/2002\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$text</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"Last&nbsp;christmas&nbsp;was&nbsp;12/24/2001\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;the&nbsp;callback&nbsp;function<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">next_year</span><span style="color: #007700">(</span><span style="color: #0000BB">$matches</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;as&nbsp;usual:&nbsp;$matches[0]&nbsp;is&nbsp;the&nbsp;complete&nbsp;match<br />&nbsp;&nbsp;//&nbsp;$matches[1]&nbsp;the&nbsp;match&nbsp;for&nbsp;the&nbsp;first&nbsp;subpattern<br />&nbsp;&nbsp;//&nbsp;enclosed&nbsp;in&nbsp;'(...)'&nbsp;and&nbsp;so&nbsp;on<br />&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">$matches</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">].(</span><span style="color: #0000BB">$matches</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">]+</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />}<br />echo&nbsp;</span><span style="color: #0000BB">preg_replace_callback</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"|(\d{2}/\d{2}/)(\d{4})|"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"next_year"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$text</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>
April fools day is 04/01/2003
Last christmas was 12/24/2002
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4796">
    <p><strong>Example #3  <span class="function"><strong>preg_replace_callback()</strong></span> using recursive structure
     to handle encapsulated BB code</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$input&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"plain&nbsp;[indent]&nbsp;deep&nbsp;[indent]&nbsp;deeper&nbsp;[/indent]&nbsp;deep&nbsp;[/indent]&nbsp;plain"</span><span style="color: #007700">;<br /><br />function&nbsp;</span><span style="color: #0000BB">parseTagsRecursive</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">)<br />{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$regex&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'#\[indent]((?:[^[]|\[(?!/?indent])|(?R))+)\[/indent]#'</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$input&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'&lt;div&nbsp;style="margin-left:&nbsp;10px"&gt;'</span><span style="color: #007700">.</span><span style="color: #0000BB">$input</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">].</span><span style="color: #DD0000">'&lt;/div&gt;'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">preg_replace_callback</span><span style="color: #007700">(</span><span style="color: #0000BB">$regex</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'parseTagsRecursive'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$output&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">parseTagsRecursive</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">$output</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.preg-replace-callback-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-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-last-error.html" class="function" rel="rdfs-seeAlso">preg_last_error()</a> - Returns the error code of the last PCRE regex execution</span></li>
    <li class="member"><a href="functions.anonymous.html" class="link">Anonymous functions</a></li>
    <li class="member">information about the <a href="language.pseudo-types.html#language.types.callback" class="link">callback</a> type</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-quote.html">preg_quote</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.preg-replace.html">preg_replace</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>