function.preg-replace-callback.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 256 行 · 第 1/2 页
HTML
256 行
</dd> </dt> <dt> <span class="term"><i><tt class="parameter">count</tt></i></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"> <h3 class="title">Return Values</h3> <p class="para"> <b>preg_replace_callback()</b> returns an array if the <i><tt class="parameter">subject</tt></i> parameter is an array, or a string otherwise. </p> <p class="para"> If matches are found, the new subject will be returned, otherwise <i><tt class="parameter">subject</tt></i> will be returned unchanged. </p> </div> <div class="refsect1 changelog"> <h3 class="title">ChangeLog</h3> <p class="para"> <table class="informaltable"> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Version</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">5.1.0</td> <td colspan="1" rowspan="1" align="left"> The <i><tt class="parameter">count</tt></i> parameter was added </td> </tr> </tbody> </colgroup> </table> </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #2 <b>preg_replace_callback()</b> example</b></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">preg_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"><pre><div class="cdata"><pre>April fools day is 04/01/2003Last christmas was 12/24/2002</pre></div> </pre></div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #3 <b>preg_replace_callback()</b> using recursive structure to handle encapsulated BB code</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$input </span><span style="color: #007700">= </span><span style="color: #DD0000">"plain [indent] deep [indent] deeper [/indent] deep [/indent] plain"</span><span style="color: #007700">;<br /><br />function </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 /> </span><span style="color: #0000BB">$regex </span><span style="color: #007700">= </span><span style="color: #DD0000">'#\[indent]((?:[^[]|\[(?!/?indent])|(?R))+)\[/indent]#'</span><span style="color: #007700">;<br /><br /> if (</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">$input </span><span style="color: #007700">= </span><span style="color: #DD0000">'<div style="margin-left: 10px">'</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">'</div>'</span><span style="color: #007700">;<br /> }<br /><br /> return </span><span style="color: #0000BB">preg_replace_callback</span><span style="color: #007700">(</span><span style="color: #0000BB">$regex</span><span style="color: #007700">, </span><span style="color: #DD0000">'parseTagsRecursive'</span><span style="color: #007700">, </span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$output </span><span style="color: #007700">= </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 </span><span style="color: #0000BB">$output</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.preg-replace.html" class="function" rel="rdfs-seeAlso">preg_replace()</a></li> <li class="member"><a href="function.create-function.html" class="function" rel="rdfs-seeAlso">create_function()</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 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>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?