URL 函数
PHP Manual

rawurlencode

(PHP 4, PHP 5)

rawurlencode按照 RFC 1738 对 URL 进行编码

说明

string rawurlencode ( string $str )

返回字符串,此字符串中除了 -_. 之外的所有非字母数字字符都将被替换成百分号(%)后跟两位十六进制数。这是在 RFC 1738 中描述的编码,是为了保护原义字符以免其被解释为特殊的 URL 定界符,同时保护 URL 格式以免其被传输媒体(像一些邮件系统)使用字符转换时弄乱。例如,如果你想在 FTP 的 URL 中包含密码:

Example #1 rawurlencode() 示例 1

<?php
echo '<a href="ftp://user:'rawurlencode('foo @+%/'),
     
'@ftp.example.com/x.txt">';
?>

或者,如果你想通过 URL 的 PATH_INFO 构成部分去传递信息:

Example #2 rawurlencode() 示例 2

<?php
echo '<a href="http://example.com/department_list_script/',
    
rawurlencode('sales and marketing/Miami'), '">';
?>

参见 rawurldecode()urldecode()urlencode()» RFC 1738

参数

str

The URL to be encoded.

返回值

Returns a string in which all non-alphanumeric characters except -_.~ have been replaced with a percent (%) sign followed by two hex digits. This is the encoding described in » RFC 3986 for protecting literal characters from being interpreted as special URL delimiters, and for protecting URLs from being mangled by transmission media with character conversions (like some email systems).

Note:

Prior to PHP 5.3.0, rawurlencode encoded tildes (~) as per » RFC 1738.

更新日志

版本 说明
5.3.4 Tilde characters are no longer encoded when rawurlencode() is used with EBCDIC strings.
5.3.0 Now conforms to » RFC 3986.

范例

Example #3 including a password in an FTP URL

<?php
echo '<a href="ftp://user:'rawurlencode('foo @+%/'),
     
'@ftp.example.com/x.txt">';
?>

以上例程会输出:

<a href="ftp://user:foo%20%40%2B%25%2F@ftp.example.com/x.txt">

Or, if you pass information in a PATH_INFO component of the URL:

Example #4 rawurlencode() example 2

<?php
echo '<a href="http://example.com/department_list_script/',
    
rawurlencode('sales and marketing/Miami'), '">';
?>

以上例程会输出:

<a href="http://example.com/department_list_script/sales%20and%20marketing%2FMiami">

参见


URL 函数
PHP Manual