📄 function.preg-replace.html
字号:
<!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</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-replace-callback.html">preg_replace_callback</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.preg-split.html">preg_split</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" class="refentry"> <div class="refnamediv"> <h1 class="refname">preg_replace</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">preg_replace</span> — <span class="dc-title">Perform a regular expression search and replace</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</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.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$replacement</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"> Searches <i><tt class="parameter">subject</tt></i> for matches to <i><tt class="parameter">pattern</tt></i> and replaces them with <i><tt class="parameter">replacement</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> <p class="para"> The <i>e</i> modifier makes <b>preg_replace()</b> treat the <i><tt class="parameter">replacement</tt></i> parameter as PHP code after the appropriate references substitution is done. Tip: make sure that <i><tt class="parameter">replacement</tt></i> constitutes a valid PHP code string, otherwise PHP will complain about a parse error at the line containing <b>preg_replace()</b>. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">replacement</tt></i></span> <dd> <p class="para"> The string or an array with strings to replace. If this parameter is a string and the <i><tt class="parameter">pattern</tt></i> parameter is an array, all patterns will be replaced by that string. If both <i><tt class="parameter">pattern</tt></i> and <i><tt class="parameter">replacement</tt></i> parameters are arrays, each <i><tt class="parameter">pattern</tt></i> will be replaced by the <i><tt class="parameter">replacement</tt></i> counterpart. If there are fewer elements in the <i><tt class="parameter">replacement</tt></i> array than in the <i><tt class="parameter">pattern</tt></i> array, any extra <i><tt class="parameter">pattern</tt></i>s will be replaced by an empty string. </p> <p class="para"> <i><tt class="parameter">replacement</tt></i> may contain references of the form <i>\\<span class="replaceable">n</span></i> or (since PHP 4.0.4) <i><span class="replaceable">$n</span></i>, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the <span class="replaceable">n</span>'th parenthesized pattern. <span class="replaceable">n </span>can be from 0 to 99, and <i>\\0</i> or <i>$0</i> refers to the text matched by the whole pattern. Opening parentheses are counted from left to right (starting from 1) to obtain the number of the capturing subpattern. </p> <p class="para"> When working with a replacement pattern where a backreference is immediately followed by another number (i.e.: placing a literal number immediately after a matched pattern), you cannot use the familiar <i>\\1</i> notation for your backreference. <i>\\11</i>, for example, would confuse <b>preg_replace()</b> since it does not know whether you want the <i>\\1</i> backreference followed by a literal <i>1</i>, or the <i>\\11</i> backreference followed by nothing. In this case the solution is to use <i>\${1}1</i>. This creates an isolated <i>$1</i> backreference, leaving the <i>1</i> as a literal. </p> <p class="para"> When using the <i>e</i> modifier, this function escapes some characters (namely <i>'</i>, <i>"</i>, <i>\</i> and NULL) in the strings that replace the backreferences. This is done to ensure that no syntax errors arise from backreference usage with either single or double quotes (e.g. <i>'strlen(\'$1\')+strlen("$2")'</i>). Make sure you are aware of PHP's <a href="language.types.string.html" class="link">string syntax</a> to know exactly how the interpreted string will look like. </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> <p class="para"> If <i><tt class="parameter">subject</tt></i> is an array, then the search and replace is performed on every entry of <i><tt class="parameter">subject</tt></i>, and the return value is an array as well. </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> </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()</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 <i><tt class="parameter">subject</tt></i> will be returned, otherwise <i><tt class="parameter">subject</tt></i> will be returned unchanged or <b><tt>NULL</tt></b> if an error occurred. </p> </div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -