Sophie

Sophie

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

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_callback</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_quote"
HREF="function.preg-quote.html"><LINK
REL="NEXT"
TITLE="preg_replace"
HREF="function.preg-replace.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-quote.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.preg-replace.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.preg-replace-callback"
></A
>preg_replace_callback</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN171830"
></A
><P
>    (PHP 4 &#62;= 4.0.5, PHP 5)</P
>preg_replace_callback&nbsp;--&nbsp;用回调函数执行正则表达式的搜索和替换</DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN171833"
></A
><H2
>说明</H2
>mixed <B
CLASS="methodname"
>preg_replace_callback</B
> ( mixed pattern, callback callback, mixed subject [, int limit] )<BR
></BR
><P
>&#13;     本函数的行为几乎和
     <A
HREF="function.preg-replace.html"
><B
CLASS="function"
>preg_replace()</B
></A
> 一样,除了不是提供一个
     <CODE
CLASS="parameter"
>replacement</CODE
> 参数,而是指定一个
     <CODE
CLASS="parameter"
>callback</CODE
> 函数。该函数将以目标字符串中的匹配数组作为输入参数,并返回用于替换的字符串。
    </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN171854"
></A
><P
><B
>例 1. <B
CLASS="function"
>preg_replace_callback()</B
> 例子</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />&nbsp;&nbsp;</font><font color="#FF8000">// 此文本是用于 2002 年的,<br />&nbsp;&nbsp;// 现在想使其能用于 2003 年<br />&nbsp;&nbsp;</font><font color="#0000BB">$text </font><font color="#007700">= </font><font color="#DD0000">"April fools day is 04/01/2002\n"</font><font color="#007700">;<br />&nbsp;&nbsp;</font><font color="#0000BB">$text</font><font color="#007700">.= </font><font color="#DD0000">"Last christmas was 12/24/2001\n"</font><font color="#007700">;<br /><br />&nbsp;&nbsp;</font><font color="#FF8000">// 回调函数<br />&nbsp;&nbsp;</font><font color="#007700">function </font><font color="#0000BB">next_year</font><font color="#007700">(</font><font color="#0000BB">$matches</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 通常:$matches[0] 是完整的匹配项<br />&nbsp;&nbsp;&nbsp;&nbsp;// $matches[1] 是第一个括号中的子模式的匹配项<br />&nbsp;&nbsp;&nbsp;&nbsp;// 以此类推<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">return </font><font color="#0000BB">$matches</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">].(</font><font color="#0000BB">$matches</font><font color="#007700">[</font><font color="#0000BB">2</font><font color="#007700">]+</font><font color="#0000BB">1</font><font color="#007700">);<br />&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;echo </font><font color="#0000BB">preg_replace_callback</font><font color="#007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"|(\d{2}/\d{2}/)(\d{4})|"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"next_year"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$text</font><font color="#007700">);<br /><br />&nbsp;&nbsp;</font><font color="#FF8000">// 结果为:<br />&nbsp;&nbsp;// April fools day is 04/01/2003<br />&nbsp;&nbsp;// Last christmas was 12/24/2002<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;     You'll often need the <CODE
CLASS="parameter"
>callback</CODE
> function
     for a <B
CLASS="function"
>preg_replace_callback()</B
> in just one place.
     In this case you can use <A
HREF="function.create-function.html"
><B
CLASS="function"
>create_function()</B
></A
> to
     declare an anonymous function as callback within the call to
     <B
CLASS="function"
>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 functions name
     not used anywhere else.
    </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN171863"
></A
><P
><B
>例 2. <B
CLASS="function"
>preg_replace_callback()</B
> 和 <A
HREF="function.create-function.html"
><B
CLASS="function"
>create_function()</B
></A
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />&nbsp;&nbsp;</font><font color="#FF8000">/* 一个 UNIX 风格的命令行过滤器,将每个段落开头的<br />&nbsp;&nbsp;&nbsp;* 大写字母转换成小写字母 */<br /><br />&nbsp;&nbsp;</font><font color="#0000BB">$fp </font><font color="#007700">= </font><font color="#0000BB">fopen</font><font color="#007700">(</font><font color="#DD0000">"php://stdin"</font><font color="#007700">, </font><font color="#DD0000">"r"</font><font color="#007700">) or die(</font><font color="#DD0000">"can't read stdin"</font><font color="#007700">);<br />&nbsp;&nbsp;while (!</font><font color="#0000BB">feof</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$line </font><font color="#007700">= </font><font color="#0000BB">fgets</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$line </font><font color="#007700">= </font><font color="#0000BB">preg_replace_callback</font><font color="#007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'|&lt;p&gt;\s*\w|'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">create_function</font><font color="#007700">(<br />&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;// 否则就把所有的 $ 换成 \$<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'$matches'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'return strtolower($matches[0]);'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$line<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#0000BB">$line</font><font color="#007700">;<br />&nbsp;&nbsp;}<br />&nbsp;&nbsp;</font><font color="#0000BB">fclose</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;     参见 <A
HREF="function.preg-replace.html"
><B
CLASS="function"
>preg_replace()</B
></A
> 和
     <A
HREF="function.create-function.html"
><B
CLASS="function"
>create_function()</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-quote.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-replace.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>preg_quote</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_replace</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>