<!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 expresssion seach and replace with multibyte support 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.mb-ereg-match.html">mb_ereg_match</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.mb-ereg-replace.html">mb_ereg_replace</a></div> <div class="up"><a href="ref.mbstring.html">Multibyte String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.mb-ereg-replace-callback" class="refentry"> <div class="refnamediv"> <h1 class="refname">mb_ereg_replace_callback</h1> <p class="verinfo">(PHP 5 >= 5.4.1)</p><p class="refpurpose"><span class="refname">mb_ereg_replace_callback</span> — <span class="dc-title">Perform a regular expresssion seach and replace with multibyte support using a callback </span></p> </div> <div class="refsect1 description" id="refsect1-function.mb-ereg-replace-callback-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>mb_ereg_replace_callback</strong></span> ( <span class="methodparam"><span class="type">string</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">string</span> <code class="parameter">$string</code></span> [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$option</code><span class="initializer"> = "msr"</span></span> ] )</div> <p class="para rdfs-comment"> Scans <em><code class="parameter">string</code></em> for matches to <em><code class="parameter">pattern</code></em>, then replaces the matched text with the output of <em><code class="parameter">callback</code></em> function. </p> <p class="para"> The behavior of this function is almost identical to <span class="function"><a href="function.mb-ereg-replace.html" class="function">mb_ereg_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.mb-ereg-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 regular expression pattern. </p> <p class="para"> Multibyte characters may be used in <em><code class="parameter">pattern</code></em>. </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. </p> <p class="para"> You'll often need the <em><code class="parameter">callback</code></em> function for a <span class="function"><strong>mb_ereg_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>mb_ereg_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's name not used anywhere else. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">string</code></em></span> <dd> <p class="para"> The <span class="type"><a href="language.types.string.html" class="type string">string</a></span> being checked. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">option</code></em></span> <dd> <p class="para"> Matching condition can be set by <em><code class="parameter">option</code></em> parameter. If <em>i</em> is specified for this parameter, the case will be ignored. If <em>x</em> is specified, white space will be ignored. If <em>m</em> is specified, match will be executed in multiline mode and line break will be included in '.'. If <em>p</em> is specified, match will be executed in POSIX mode, line break will be considered as normal character. Note that <em>e</em> cannot be used for <span class="function"><strong>mb_ereg_replace_callback()</strong></span>. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.mb-ereg-replace-callback-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> The resultant <span class="type"><a href="language.types.string.html" class="type string">string</a></span> on success, or <strong><code>FALSE</code></strong> on error. </p> </div> <div class="refsect1 notes" id="refsect1-function.mb-ereg-replace-callback-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">The internal encoding or the character encoding specified by <span class="function"><a href="function.mb-regex-encoding.html" class="function">mb_regex_encoding()</a></span> will be used as the character encoding for this function.</p></p></blockquote> </div> <div class="refsect1 examples" id="refsect1-function.mb-ereg-replace-callback-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-2712"> <p><strong>Example #1 <span class="function"><strong>mb_ereg_replace_callback()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// this text was used in 2002<br />// we want to get this up to date for 2003<br /></span><span style="color: #0000BB">$text </span><span style="color: #007700">= </span><span style="color: #DD0000">"April fools day is 04/01/2002\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$text</span><span style="color: #007700">.= </span><span style="color: #DD0000">"Last christmas was 12/24/2001\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// the callback function<br /></span><span style="color: #007700">function </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 /> </span><span style="color: #FF8000">// as usual: $matches[0] is the complete match<br /> // $matches[1] the match for the first subpattern<br /> // enclosed in '(...)' and so on<br /> </span><span style="color: #007700">return </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 </span><span style="color: #0000BB">mb_ereg_replace_callback</span><span style="color: #007700">(<br /> </span><span style="color: #DD0000">"(\d{2}/\d{2}/)(\d{4})"</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">"next_year"</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">$text</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></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-2713"> <p><strong>Example #2 <span class="function"><strong>mb_ereg_replace_callback()</strong></span> using anonymous function supported in PHP 5.3.0 or later</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// this text was used in 2002<br />// we want to get this up to date for 2003<br /></span><span style="color: #0000BB">$text </span><span style="color: #007700">= </span><span style="color: #DD0000">"April fools day is 04/01/2002\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$text</span><span style="color: #007700">.= </span><span style="color: #DD0000">"Last christmas was 12/24/2001\n"</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">mb_ereg_replace_callback</span><span style="color: #007700">(<br /> </span><span style="color: #DD0000">"(\d{2}/\d{2}/)(\d{4})"</span><span style="color: #007700">,<br /> function (</span><span style="color: #0000BB">$matches</span><span style="color: #007700">) {<br /> return </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 /> </span><span style="color: #0000BB">$text</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.mb-ereg-replace-callback-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.mb-regex-encoding.html" class="function" rel="rdfs-seeAlso">mb_regex_encoding()</a> - Set/Get character encoding for multibyte regex</span></li> <li class="member"> <span class="function"><a href="function.mb-ereg-replace.html" class="function" rel="rdfs-seeAlso">mb_ereg_replace()</a> - Replace regular expression with multibyte support</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.mb-ereg-match.html">mb_ereg_match</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.mb-ereg-replace.html">mb_ereg_replace</a></div> <div class="up"><a href="ref.mbstring.html">Multibyte String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>