function.preg-replace-callback.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 256 行 · 第 1/2 页
HTML
256 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Perform a regular expression search and replace using a callback</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><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><hr /><div id="function.preg-replace-callback" class="refentry"> <div class="refnamediv"> <h1 class="refname">preg_replace_callback</h1> <p class="verinfo">(PHP 4 >= 4.0.5, PHP 5)</p><p class="refpurpose"><span class="refname">preg_replace_callback</span> — <span class="dc-title">Perform a regular expression search and replace using a callback</span></p> </div> <div class="refsect1 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"><b><b>preg_replace_callback</b></b></span> ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$pattern</tt></span> , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.callback" class="type callback">callback</a></span> <tt class="parameter">$callback</tt></span> , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$subject</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$limit</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter reference">&$count</tt></span> ]] )</div> <p class="para rdfs-comment"> The behavior of this function is almost identical to <a href="function.preg-replace.html" class="function">preg_replace()</a>, except for the fact that instead of <i><tt class="parameter">replacement</tt></i> parameter, one should specify a <i><tt class="parameter">callback</tt></i>. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">pattern</tt></i></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"><i><tt class="parameter">callback</tt></i></span> <dd> <p class="para"> A callback that will be called and passed an array of matched elements in the <i><tt class="parameter">subject</tt></i> string. The callback should return the replacement string. </p> <p class="para"> You'll often need the <i><tt class="parameter">callback</tt></i> function for a <b>preg_replace_callback()</b> in just one place. In this case you can use <a href="function.create-function.html" class="function">create_function()</a> to declare an anonymous function as callback within the call to <b>preg_replace_callback()</b>. 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> <p class="para"> <div class="example"> <p><b>Example #1 <b>preg_replace_callback()</b> and <a href="function.create-function.html" class="function">create_function()</a></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">/* a unix-style command line filter to convert uppercase<br /> * letters at the beginning of paragraphs to lowercase */<br /></span><span style="color: #0000BB">$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"php://stdin"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">) or die(</span><span style="color: #DD0000">"can't read stdin"</span><span style="color: #007700">);<br />while (!</span><span style="color: #0000BB">feof</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">$line </span><span style="color: #007700">= </span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$line </span><span style="color: #007700">= </span><span style="color: #0000BB">preg_replace_callback</span><span style="color: #007700">(<br /> </span><span style="color: #DD0000">'|<p>\s*\w|'</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">create_function</span><span style="color: #007700">(<br /> </span><span style="color: #FF8000">// single quotes are essential here,<br /> // or alternative escape all $ as \$<br /> </span><span style="color: #DD0000">'$matches'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'return strtolower($matches[0]);'<br /> </span><span style="color: #007700">),<br /> </span><span style="color: #0000BB">$line<br /> </span><span style="color: #007700">);<br /> echo </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">?></span></span></code></div> </div> </div> </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">subject</tt></i></span> <dd> <p class="para"> The string or an array with strings to search and replace. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">limit</tt></i></span> <dd> <p class="para"> The maximum possible replacements for each pattern in each <i><tt class="parameter">subject</tt></i> string. Defaults to <i>-1</i> (no limit). </p>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?