Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > by-pkgid > 05cd670d8a02b2b4a0ffb1756f2e8308 > files > 3834

php-manual-zh-5.2.4-1mdv2008.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>preg_replace</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="PHP 手册"
HREF="index.html"><LINK
REL="UP"
TITLE="Perl 兼容正则表达式函数"
HREF="ref.pcre.html"><LINK
REL="PREVIOUS"
TITLE="preg_replace_callback"
HREF="function.preg-replace-callback.html"><LINK
REL="NEXT"
TITLE="preg_split"
HREF="function.preg-split.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=UTF-8"></HEAD
><BODY
CLASS="refentry"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>PHP 手册</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.preg-replace-callback.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.preg-split.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.preg-replace"
></A
>preg_replace</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN171872"
></A
><P
>    (PHP 3 &#62;= 3.0.9, PHP 4, PHP 5)</P
>preg_replace&nbsp;--&nbsp;执行正则表达式的搜索和替换</DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN171875"
></A
><H2
>说明</H2
>mixed <B
CLASS="methodname"
>preg_replace</B
> ( mixed pattern, mixed replacement, mixed subject [, int limit] )<BR
></BR
><P
>&#13;     在
     <CODE
CLASS="parameter"
>subject</CODE
> 中搜索
     <CODE
CLASS="parameter"
>pattern</CODE
> 模式的匹配项并替换为
     <CODE
CLASS="parameter"
>replacement</CODE
>。如果指定了
     <CODE
CLASS="parameter"
>limit</CODE
>,则仅替换
     <CODE
CLASS="parameter"
>limit</CODE
> 个匹配,如果省略
     <CODE
CLASS="parameter"
>limit</CODE
> 或者其值为 -1,则所有的匹配项都会被替换。
    </P
><P
>&#13;     <CODE
CLASS="parameter"
>replacement</CODE
> 可以包含
     <TT
CLASS="literal"
>\\<TT
CLASS="replaceable"
><I
>n</I
></TT
></TT
>
     形式或(自 PHP 4.0.4 起)<TT
CLASS="literal"
><TT
CLASS="replaceable"
><I
>$n</I
></TT
></TT
>
     形式的逆向引用,首选使用后者。每个此种引用将被替换为与第
     <TT
CLASS="replaceable"
><I
>n</I
></TT
>
     个被捕获的括号内的子模式所匹配的文本。<TT
CLASS="replaceable"
><I
>n</I
></TT
>
     可以从 0 到 99,其中
     <TT
CLASS="literal"
>\\0</TT
> 或 <TT
CLASS="literal"
>$0</TT
>
     指的是被整个模式所匹配的文本。对左圆括号从左到右计数(从 1 开始)以取得子模式的数目。
    </P
><P
>&#13;     对替换模式在一个逆向引用后面紧接着一个数字时(即:紧接在一个匹配的模式后面的数字),不能使用熟悉的
     <TT
CLASS="literal"
>\\1</TT
> 符号来表示逆向引用。举例说
     <TT
CLASS="literal"
>\\11</TT
>,将会使
     <B
CLASS="function"
>preg_replace()</B
> 搞不清楚是想要一个
     <TT
CLASS="literal"
>\\1</TT
> 的逆向引用后面跟着一个数字
     <TT
CLASS="literal"
>1</TT
> 还是一个
     <TT
CLASS="literal"
>\\11</TT
> 的逆向引用。本例中的解决方法是使用
     <TT
CLASS="literal"
>\${1}1</TT
>。这会形成一个隔离的
     <TT
CLASS="literal"
>$1</TT
> 逆向引用,而使另一个
     <TT
CLASS="literal"
>1</TT
> 只是单纯的文字。
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN171920"
></A
><P
><B
>例 1. 逆向引用后面紧接着数字的用法</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$string </font><font color="#007700">= </font><font color="#DD0000">"April 15, 2003"</font><font color="#007700">;<br /></font><font color="#0000BB">$pattern </font><font color="#007700">= </font><font color="#DD0000">"/(\w+) (\d+), (\d+)/i"</font><font color="#007700">;<br /></font><font color="#0000BB">$replacement </font><font color="#007700">= </font><font color="#DD0000">"\${1}1,\$3"</font><font color="#007700">;<br />print </font><font color="#0000BB">preg_replace</font><font color="#007700">(</font><font color="#0000BB">$pattern</font><font color="#007700">, </font><font color="#0000BB">$replacement</font><font color="#007700">, </font><font color="#0000BB">$string</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Output<br />&nbsp;&nbsp;&nbsp;======<br /><br />April1,2003<br /><br /> */<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     如果搜索到匹配项,则会返回被替换后的
     <CODE
CLASS="parameter"
>subject</CODE
>,否则返回原来不变的
     <CODE
CLASS="parameter"
>subject</CODE
>。
    </P
><P
>&#13;     <B
CLASS="function"
>preg_replace()</B
> 的每个参数(除了
     <CODE
CLASS="parameter"
>limit</CODE
>)都可以是一个数组。如果
     <CODE
CLASS="parameter"
>pattern</CODE
> 和
     <CODE
CLASS="parameter"
>replacement</CODE
> 都是数组,将以其键名在数组中出现的顺序来进行处理。这<SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>不一定</I
></SPAN
>和索引的数字顺序相同。如果使用索引来标识哪个
     <CODE
CLASS="parameter"
>pattern</CODE
> 将被哪个
     <CODE
CLASS="parameter"
>replacement</CODE
> 来替换,应该在调用
     <B
CLASS="function"
>preg_replace()</B
> 之前用
     <A
HREF="function.ksort.html"
><B
CLASS="function"
>ksort()</B
></A
> 对数组进行排序。
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN171937"
></A
><P
><B
>例 2. 在 <B
CLASS="function"
>preg_replace()</B
> 中使用索引数组</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$string </font><font color="#007700">= </font><font color="#DD0000">"The quick brown fox jumped over the lazy dog."</font><font color="#007700">;<br /><br /></font><font color="#0000BB">$patterns</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">] = </font><font color="#DD0000">"/quick/"</font><font color="#007700">;<br /></font><font color="#0000BB">$patterns</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">] = </font><font color="#DD0000">"/brown/"</font><font color="#007700">;<br /></font><font color="#0000BB">$patterns</font><font color="#007700">[</font><font color="#0000BB">2</font><font color="#007700">] = </font><font color="#DD0000">"/fox/"</font><font color="#007700">;<br /><br /></font><font color="#0000BB">$replacements</font><font color="#007700">[</font><font color="#0000BB">2</font><font color="#007700">] = </font><font color="#DD0000">"bear"</font><font color="#007700">;<br /></font><font color="#0000BB">$replacements</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">] = </font><font color="#DD0000">"black"</font><font color="#007700">;<br /></font><font color="#0000BB">$replacements</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">] = </font><font color="#DD0000">"slow"</font><font color="#007700">;<br /><br />print </font><font color="#0000BB">preg_replace</font><font color="#007700">(</font><font color="#0000BB">$patterns</font><font color="#007700">, </font><font color="#0000BB">$replacements</font><font color="#007700">, </font><font color="#0000BB">$string</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Output<br />&nbsp;&nbsp;&nbsp;======<br /><br />The bear black slow jumped over the lazy dog.<br /><br />*/<br /><br />/* By ksorting patterns and replacements,<br />&nbsp;&nbsp;&nbsp;we should get what we wanted. */<br /><br /></font><font color="#0000BB">ksort</font><font color="#007700">(</font><font color="#0000BB">$patterns</font><font color="#007700">);<br /></font><font color="#0000BB">ksort</font><font color="#007700">(</font><font color="#0000BB">$replacements</font><font color="#007700">);<br /><br />print </font><font color="#0000BB">preg_replace</font><font color="#007700">(</font><font color="#0000BB">$patterns</font><font color="#007700">, </font><font color="#0000BB">$replacements</font><font color="#007700">, </font><font color="#0000BB">$string</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Output<br />&nbsp;&nbsp;&nbsp;======<br /><br />The slow black bear jumped over the lazy dog.<br /><br />*/<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     如果
     <CODE
CLASS="parameter"
>subject</CODE
> 是个数组,则会对
     <CODE
CLASS="parameter"
>subject</CODE
> 中的每个项目执行搜索和替换,并返回一个数组。
    </P
><P
>&#13;     如果
     <CODE
CLASS="parameter"
>pattern</CODE
> 和
     <CODE
CLASS="parameter"
>replacement</CODE
> 都是数组,则
     <B
CLASS="function"
>preg_replace()</B
> 会依次从中分别取出值来对
     <CODE
CLASS="parameter"
>subject</CODE
> 进行搜索和替换。如果
     <CODE
CLASS="parameter"
>replacement</CODE
> 中的值比
     <CODE
CLASS="parameter"
>pattern</CODE
> 中的少,则用空字符串作为余下的替换值。如果
     <CODE
CLASS="parameter"
>pattern</CODE
> 是数组而
     <CODE
CLASS="parameter"
>replacement</CODE
> 是字符串,则对
     <CODE
CLASS="parameter"
>pattern</CODE
> 中的每个值都用此字符串作为替换值。反过来则没有意义了。
    </P
><P
>&#13;     <TT
CLASS="literal"
>/e</TT
> 修正符使
     <B
CLASS="function"
>preg_replace()</B
> 将
     <CODE
CLASS="parameter"
>replacement</CODE
> 参数当作
     PHP 代码(在适当的逆向引用替换完之后)。提示:要确保
     <CODE
CLASS="parameter"
>replacement</CODE
> 构成一个合法的
     PHP 代码字符串,否则
     PHP 会在报告在包含
     <B
CLASS="function"
>preg_replace()</B
> 的行中出现语法解析错误。
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN171961"
></A
><P
><B
>例 3. 替换数个值</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$patterns </font><font color="#007700">= array (</font><font color="#DD0000">"/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"/^\s*{(\w+)}\s*=/"</font><font color="#007700">);<br /></font><font color="#0000BB">$replace </font><font color="#007700">= array (</font><font color="#DD0000">"\\3/\\4/\\1\\2"</font><font color="#007700">, </font><font color="#DD0000">"</font><font color="#007700">$\\</font><font color="#DD0000">1 ="</font><font color="#007700">);<br />print </font><font color="#0000BB">preg_replace </font><font color="#007700">(</font><font color="#0000BB">$patterns</font><font color="#007700">, </font><font color="#0000BB">$replace</font><font color="#007700">, </font><font color="#DD0000">"{startDate} = 1999-5-27"</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>&#13;       本例将输出:
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>$startDate = 5/27/1999</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN171967"
></A
><P
><B
>例 4. 使用 /e 修正符</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />preg_replace </font><font color="#007700">(</font><font color="#DD0000">"/(&lt;\/?)(\w+)([^&gt;]*&gt;)/e"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"'\\1'.strtoupper('\\2').'\\3'"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$html_body</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>&#13;       这将使输入字符串中的所有 HTML 标记变成大写。
      </P
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN171972"
></A
><P
><B
>例 5. 将 HTML 转换成文本</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">// $document 应包含一个 HTML 文档。<br />// 本例将去掉 HTML 标记,javascript 代码<br />// 和空白字符。还会将一些通用的<br />// HTML 实体转换成相应的文本。<br /><br /></font><font color="#0000BB">$search </font><font color="#007700">= array (</font><font color="#DD0000">"'&lt;script[^&gt;]*?&gt;.*?&lt;/script&gt;'si"</font><font color="#007700">,&nbsp;&nbsp;</font><font color="#FF8000">// 去掉 javascript<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"'&lt;[\/\!]*?[^&lt;&gt;]*?&gt;'si"</font><font color="#007700">,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 去掉 HTML 标记<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"'([\r\n])[\s]+'"</font><font color="#007700">,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 去掉空白字符<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"'&amp;(quot|#34);'i"</font><font color="#007700">,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 替换 HTML 实体<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"'&amp;(amp|#38);'i"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"'&amp;(lt|#60);'i"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"'&amp;(gt|#62);'i"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"'&amp;(nbsp|#160);'i"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"'&amp;(iexcl|#161);'i"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"'&amp;(cent|#162);'i"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"'&amp;(pound|#163);'i"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"'&amp;(copy|#169);'i"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"'&amp;#(\d+);'e"</font><font color="#007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 作为 PHP 代码运行<br /><br /></font><font color="#0000BB">$replace </font><font color="#007700">= array (</font><font color="#DD0000">""</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">""</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"\\1"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"\""</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"&amp;"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"&lt;"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"&gt;"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">" "</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">chr</font><font color="#007700">(</font><font color="#0000BB">161</font><font color="#007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">chr</font><font color="#007700">(</font><font color="#0000BB">162</font><font color="#007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">chr</font><font color="#007700">(</font><font color="#0000BB">163</font><font color="#007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">chr</font><font color="#007700">(</font><font color="#0000BB">169</font><font color="#007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"chr(\\1)"</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$text </font><font color="#007700">= </font><font color="#0000BB">preg_replace </font><font color="#007700">(</font><font color="#0000BB">$search</font><font color="#007700">, </font><font color="#0000BB">$replace</font><font color="#007700">, </font><font color="#0000BB">$document</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
      <CODE
CLASS="parameter"
>limit</CODE
> 参数是 PHP 4.0.1pl2 之后加入的。
     </P
></BLOCKQUOTE
></DIV
><P
>&#13;     参见 <A
HREF="function.preg-match.html"
><B
CLASS="function"
>preg_match()</B
></A
>,<A
HREF="function.preg-match-all.html"
><B
CLASS="function"
>preg_match_all()</B
></A
>
     和 <A
HREF="function.preg-split.html"
><B
CLASS="function"
>preg_split()</B
></A
>。
    </P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="function.preg-replace-callback.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>起始页</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="function.preg-split.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>preg_replace_callback</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="ref.pcre.html"
ACCESSKEY="U"
>上一级</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>preg_split</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>