Sophie

Sophie

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

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
>str_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="String 字符串处理函数"
HREF="ref.strings.html"><LINK
REL="PREVIOUS"
TITLE="str_repeat"
HREF="function.str-repeat.html"><LINK
REL="NEXT"
TITLE="str_rot13"
HREF="function.str-rot13.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.str-repeat.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.str-rot13.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.str-replace"
></A
>str_replace</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN227150"
></A
><P
>    (PHP 3 &#62;= 3.0.6, PHP 4, PHP 5)</P
>str_replace&nbsp;--&nbsp;
     Replace all occurrences of the search string with the replacement string
    </DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN227153"
></A
><H2
>Description</H2
>mixed <B
CLASS="methodname"
>str_replace</B
> ( mixed search, mixed replace, mixed subject [, int &#38;count] )<BR
></BR
><P
>&#13;     This function returns a string or an array with all occurrences of
     <CODE
CLASS="parameter"
>search</CODE
> in <CODE
CLASS="parameter"
>subject</CODE
>
     replaced with the given <CODE
CLASS="parameter"
>replace</CODE
> value.  If you
     don't need fancy replacing rules (like regular expressions), you should
     always use this function instead of <A
HREF="function.ereg-replace.html"
><B
CLASS="function"
>ereg_replace()</B
></A
> or
     <A
HREF="function.preg-replace.html"
><B
CLASS="function"
>preg_replace()</B
></A
>.
    </P
><P
>&#13;     As of PHP 4.0.5, every parameter in <B
CLASS="function"
>str_replace()</B
>
     can be an <A
HREF="language.types.array.html"
><B
CLASS="type"
>array</B
></A
>. 
    </P
><DIV
CLASS="warning"
><P
></P
><TABLE
CLASS="warning"
BORDER="1"
WIDTH="100%"
><TR
><TD
ALIGN="CENTER"
><B
>警告</B
></TD
></TR
><TR
><TD
ALIGN="LEFT"
><P
>&#13;      In PHP versions prior to 4.3.3 a bug existed when using arrays as
      both <CODE
CLASS="parameter"
>search</CODE
> and <CODE
CLASS="parameter"
>replace</CODE
>
      parameters which caused empty <CODE
CLASS="parameter"
>search</CODE
> indexes
      to be skipped without advancing the internal pointer on the
      <CODE
CLASS="parameter"
>replace</CODE
> array. This has been corrected in
      PHP 4.3.3, any scripts which relied on this bug
      should remove empty search values prior to calling this function in
      order to mimick the original behavior.
     </P
></TD
></TR
></TABLE
></DIV
><P
>&#13;     If <CODE
CLASS="parameter"
>subject</CODE
> is an array, then the search
     and replace is performed with every entry of
     <CODE
CLASS="parameter"
>subject</CODE
>, and the return value is an array
     as well.
    </P
><P
>&#13;     If <CODE
CLASS="parameter"
>search</CODE
> and
     <CODE
CLASS="parameter"
>replace</CODE
> are arrays, then
     <B
CLASS="function"
>str_replace()</B
> takes a value from each array
     and uses them to do search and replace on
     <CODE
CLASS="parameter"
>subject</CODE
>. If
     <CODE
CLASS="parameter"
>replace</CODE
> has fewer values than
     <CODE
CLASS="parameter"
>search</CODE
>, then an empty string is used for
     the rest of replacement values. If <CODE
CLASS="parameter"
>search</CODE
>
     is an array and <CODE
CLASS="parameter"
>replace</CODE
> is a string, then
     this replacement string is used for every value of
     <CODE
CLASS="parameter"
>search</CODE
>. The converse would not make sense,
     though.
    </P
><P
>&#13;     If <CODE
CLASS="parameter"
>search</CODE
> or <CODE
CLASS="parameter"
>replace</CODE
>
     are arrays, their elements are processed first to last.
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN227202"
></A
><P
><B
>例 1. <B
CLASS="function"
>str_replace()</B
> examples</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">// Provides: &lt;body text='black'&gt;<br /></font><font color="#0000BB">$bodytag </font><font color="#007700">= </font><font color="#0000BB">str_replace</font><font color="#007700">(</font><font color="#DD0000">"%body%"</font><font color="#007700">, </font><font color="#DD0000">"black"</font><font color="#007700">, </font><font color="#DD0000">"&lt;body text='%body%'&gt;"</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Provides: Hll Wrld f PHP<br /></font><font color="#0000BB">$vowels </font><font color="#007700">= array(</font><font color="#DD0000">"a"</font><font color="#007700">, </font><font color="#DD0000">"e"</font><font color="#007700">, </font><font color="#DD0000">"i"</font><font color="#007700">, </font><font color="#DD0000">"o"</font><font color="#007700">, </font><font color="#DD0000">"u"</font><font color="#007700">, </font><font color="#DD0000">"A"</font><font color="#007700">, </font><font color="#DD0000">"E"</font><font color="#007700">, </font><font color="#DD0000">"I"</font><font color="#007700">, </font><font color="#DD0000">"O"</font><font color="#007700">, </font><font color="#DD0000">"U"</font><font color="#007700">);<br /></font><font color="#0000BB">$onlyconsonants </font><font color="#007700">= </font><font color="#0000BB">str_replace</font><font color="#007700">(</font><font color="#0000BB">$vowels</font><font color="#007700">, </font><font color="#DD0000">""</font><font color="#007700">, </font><font color="#DD0000">"Hello World of PHP"</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Provides: You should eat pizza, beer, and ice cream every day<br /></font><font color="#0000BB">$phrase&nbsp;&nbsp;</font><font color="#007700">= </font><font color="#DD0000">"You should eat fruits, vegetables, and fiber every day."</font><font color="#007700">;<br /></font><font color="#0000BB">$healthy </font><font color="#007700">= array(</font><font color="#DD0000">"fruits"</font><font color="#007700">, </font><font color="#DD0000">"vegetables"</font><font color="#007700">, </font><font color="#DD0000">"fiber"</font><font color="#007700">);<br /></font><font color="#0000BB">$yummy&nbsp;&nbsp;&nbsp;</font><font color="#007700">= array(</font><font color="#DD0000">"pizza"</font><font color="#007700">, </font><font color="#DD0000">"beer"</font><font color="#007700">, </font><font color="#DD0000">"ice cream"</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$newphrase </font><font color="#007700">= </font><font color="#0000BB">str_replace</font><font color="#007700">(</font><font color="#0000BB">$healthy</font><font color="#007700">, </font><font color="#0000BB">$yummy</font><font color="#007700">, </font><font color="#0000BB">$phrase</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Use of the count parameter is available as of PHP 5.0.0<br /></font><font color="#0000BB">$str </font><font color="#007700">= </font><font color="#0000BB">str_replace</font><font color="#007700">(</font><font color="#DD0000">"ll"</font><font color="#007700">, </font><font color="#DD0000">""</font><font color="#007700">, </font><font color="#DD0000">"good golly miss molly!"</font><font color="#007700">, </font><font color="#0000BB">$count</font><font color="#007700">);<br />echo </font><font color="#0000BB">$count</font><font color="#007700">; </font><font color="#FF8000">// 2<br /><br />// Order of replacement <br /></font><font color="#0000BB">$str&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">= </font><font color="#DD0000">"Line 1\nLine 2\rLine 3\r\nLine 4\n"</font><font color="#007700">;<br /></font><font color="#0000BB">$order&nbsp;&nbsp;&nbsp;</font><font color="#007700">= array(</font><font color="#DD0000">"\r\n"</font><font color="#007700">, </font><font color="#DD0000">"\n"</font><font color="#007700">, </font><font color="#DD0000">"\r"</font><font color="#007700">);<br /></font><font color="#0000BB">$replace </font><font color="#007700">= </font><font color="#DD0000">'&lt;br /&gt;'</font><font color="#007700">;<br /></font><font color="#FF8000">// Processes \r\n's first so they aren't converted twice. <br /></font><font color="#0000BB">$newstr </font><font color="#007700">= </font><font color="#0000BB">str_replace</font><font color="#007700">(</font><font color="#0000BB">$order</font><font color="#007700">, </font><font color="#0000BB">$replace</font><font color="#007700">, </font><font color="#0000BB">$str</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Outputs: apearpearle pear<br /></font><font color="#0000BB">$letters </font><font color="#007700">= array(</font><font color="#DD0000">'a'</font><font color="#007700">, </font><font color="#DD0000">'p'</font><font color="#007700">);<br /></font><font color="#0000BB">$fruit&nbsp;&nbsp;&nbsp;</font><font color="#007700">= array(</font><font color="#DD0000">'apple'</font><font color="#007700">, </font><font color="#DD0000">'pear'</font><font color="#007700">);<br /></font><font color="#0000BB">$text&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">= </font><font color="#DD0000">'a p'</font><font color="#007700">;<br /></font><font color="#0000BB">$output&nbsp;&nbsp;</font><font color="#007700">= </font><font color="#0000BB">str_replace</font><font color="#007700">(</font><font color="#0000BB">$letters</font><font color="#007700">, </font><font color="#0000BB">$fruit</font><font color="#007700">, </font><font color="#0000BB">$text</font><font color="#007700">);<br />echo </font><font color="#0000BB">$output</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
>本函数可安全用于二进制对象。</P
></BLOCKQUOTE
></DIV
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
      This function is case-sensitive. Use <A
HREF="function.str-ireplace.html"
><B
CLASS="function"
>str_ireplace()</B
></A
>
      for case-insensitive replace.
     </P
></BLOCKQUOTE
></DIV
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
      As of PHP 5.0.0 the number of matched and replaced
      needles (<CODE
CLASS="parameter"
>search</CODE
>) will be returned in
      <CODE
CLASS="parameter"
>count</CODE
> which is passed by reference.
      Prior to PHP 5.0.0 this parameter is not available.
     </P
></BLOCKQUOTE
></DIV
><P
>&#13;     See also 
     <A
HREF="function.str-ireplace.html"
><B
CLASS="function"
>str_ireplace()</B
></A
>,
     <A
HREF="function.substr-replace.html"
><B
CLASS="function"
>substr_replace()</B
></A
>,
     <A
HREF="function.preg-replace.html"
><B
CLASS="function"
>preg_replace()</B
></A
>, and 
     <A
HREF="function.strtr.html"
><B
CLASS="function"
>strtr()</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.str-repeat.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.str-rot13.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>str_repeat</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="ref.strings.html"
ACCESSKEY="U"
>上一级</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>str_rot13</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>