<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >urlencode</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="URL 函数" HREF="ref.url.html"><LINK REL="PREVIOUS" TITLE="urldecode" HREF="function.urldecode.html"><LINK REL="NEXT" TITLE="Variable 变量函数" HREF="ref.var.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.urldecode.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="ref.var.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.urlencode" ></A >urlencode</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN235977" ></A ><P > (PHP 3, PHP 4, PHP 5)</P >urlencode -- 编码 URL 字符串</DIV ><DIV CLASS="refsect1" ><A NAME="AEN235980" ></A ><H2 >说明</H2 >string <B CLASS="methodname" >urlencode</B > ( string str )<BR ></BR ><P > 返回字符串,此字符串中除了 <TT CLASS="literal" >-_.</TT > 之外的所有非字母数字字符都将被替换成百分号(<TT CLASS="literal" >%</TT >)后跟两位十六进制数,空格则编码为加号(<TT CLASS="literal" >+</TT >)。此编码与 WWW 表单 POST 数据的编码方式是一样的,同时与 <TT CLASS="literal" >application/x-www-form-urlencoded</TT > 的媒体类型编码方式一样。由于历史原因,此编码在将空格编码为加号(+)方面与 RFC1738 编码(参见 <A HREF="function.rawurlencode.html" ><B CLASS="function" >rawurlencode()</B ></A >)不同。此函数便于将字符串编码并将其用于 URL 的请求部分,同时它还便于将变量传递给下一页: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN235994" ></A ><P ><B >例 1. <B CLASS="function" >urlencode()</B > 例子</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">echo </font><font color="#DD0000">'<a href="mycgi?foo='</font><font color="#007700">, </font><font color="#0000BB">urlencode</font><font color="#007700">(</font><font color="#0000BB">$userinput</font><font color="#007700">), </font><font color="#DD0000">'">'</font><font color="#007700">;<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > 注意:小心与 HTML 实体相匹配的变量。像 &amp、&copy 和 &pound 都将被浏览器解析,并使用实际实体替代所期待的变量名。这是明显的混乱,W3C 已经告诫人们好几年了。参考地址:<A HREF="http://www.w3.org/TR/html4/appendix/notes.html#h-B.2.2" TARGET="_top" >http://www.w3.org/TR/html4/appendix/notes.html#h-B.2.2</A >。PHP 通过 arg_separator.ini 指令,支持将参数分割符变成 W3C 所建议的分号。不幸的是大多数用户代理并不发送分号分隔符格式的表单数据。较为简单的解决办法是使用 &amp; 代替 & 作为分隔符。你不需要为此修改 PHP 的 arg_separator。让它仍为 &,而仅使用 <A HREF="function.htmlentities.html" ><B CLASS="function" >htmlentities()</B ></A > 或 <A HREF="function.htmlspecialchars.html" ><B CLASS="function" >htmlspecialchars()</B ></A > 对你的 URL 进行编码。 </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN236003" ></A ><P ><B >例 2. <B CLASS="function" >urlencode()</B > 与 <A HREF="function.htmlentities.html" ><B CLASS="function" >htmlentities()</B ></A > 例子</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$query_string </font><font color="#007700">= </font><font color="#DD0000">'foo=' </font><font color="#007700">. </font><font color="#0000BB">urlencode</font><font color="#007700">(</font><font color="#0000BB">$foo</font><font color="#007700">) . </font><font color="#DD0000">'&bar=' </font><font color="#007700">. </font><font color="#0000BB">urlencode</font><font color="#007700">(</font><font color="#0000BB">$bar</font><font color="#007700">);<br />echo </font><font color="#DD0000">'<a href="mycgi?' </font><font color="#007700">. </font><font color="#0000BB">htmlentities</font><font color="#007700">(</font><font color="#0000BB">$query_string</font><font color="#007700">) . </font><font color="#DD0000">'">'</font><font color="#007700">;<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > 参见 <A HREF="function.urldecode.html" ><B CLASS="function" >urldecode()</B ></A >,<A HREF="function.htmlentities.html" ><B CLASS="function" >htmlentities()</B ></A >,<A HREF="function.rawurldecode.html" ><B CLASS="function" >rawurldecode()</B ></A > 和 <A HREF="function.rawurlencode.html" ><B CLASS="function" >rawurlencode()</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.urldecode.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="ref.var.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >urldecode</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.url.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Variable 变量函数</TD ></TR ></TABLE ></DIV ></BODY ></HTML >