urlencode

本文详细介绍了PHP中的urlencode函数,用于URL编码字符串。文章解释了如何使用此函数来编码URL中的变量,并给出了具体的示例代码,同时对比了其与RFC 3986标准中的差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

urlencode

(PHP 4, PHP 5)

urlencode — URL-encodes string

reject note Description

string  urlencode (  string $str )

This function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass variables to the next page.

reject note Parameters

str

The string to be encoded.

reject note Return Values

Returns a string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs. It is encoded the same way that the posted data from a WWW form is encoded, that is the same way as in application/x-www-form-urlencoded media type. This differs from the » RFC 3986 encoding (see rawurlencode()) in that for historical reasons, spaces are encoded as plus (+) signs.

reject note Examples

Example #1 urlencode() example

<?php
echo '<a href="mycgi?foo='urlencode($userinput), '">';
?>

Example #2 urlencode() and htmlentities() example

<?php
$query_string 
'foo=' urlencode($foo) . '&bar=' urlencode($bar);
echo 
'<a href="mycgi?' htmlentities($query_string) . '">';
?>

reject note Notes

Note:

Be careful about variables that may match HTML entities. Things like &amp, &copy and &pound are parsed by the browser and the actual entity is used instead of the desired variable name. This is an obvious hassle that the W3C has been telling people about for years. The reference is here: » http://www.w3.org/TR/html4/appendix/notes.html#h-B.2.2.

PHP supports changing the argument separator to the W3C-suggested semi-colon through the arg_separator .ini directive. Unfortunately most user agents do not send form data in this semi-colon separated format. A more portable way around this is to use &amp; instead of & as the separator. You don't need to change PHP's arg_separator for this. Leave it as &, but simply encode your URLs using htmlentities() or htmlspecialchars().

reject note See Also

在编程中,`urlEncode`(URL编码)是一种将字符串转换为 URL 安全格式的过程。URL 编码会将非 ASCII 字符或特殊字符转换为 `%` 后跟两位十六进制数的形式。例如,空格会被转换为 `%20`。 以下是使用 Python 实现 `urlEncode` 的示例代码: ```python import urllib.parse def url_encode(input_string): encoded_string = urllib.parse.quote(input_string) return encoded_string # 示例 input_str = "Hello World! 欢迎来到编程世界" encoded_str = url_encode(input_str) print("Encoded String:", encoded_str) ``` ### 解释 - `urllib.parse.quote` 是 Python 标准库中用于 URL 编码的函数。 - 它会将输入字符串中的特殊字符(如空格、中文字符等)转换为对应的百分号编码形式。 - 例如,空格会被转换为 `%20`,而中文字符也会被转换为 UTF-8 编码的百分号形式。 以下是使用 JavaScript 实现 `urlEncode` 的示例代码: ```javascript function urlEncode(inputString) { return encodeURIComponent(inputString); } // 示例 const inputStr = "Hello World! 欢迎来到编程世界"; const encodedStr = urlEncode(inputStr); console.log("Encoded String:", encodedStr); ``` ### 解释 - `encodeURIComponent` 是 JavaScript 中用于 URL 编码的函数。 - 它会将输入字符串中的所有特殊字符进行编码,包括空格、中文字符等。 - 例如,空格会被转换为 `%20`,而中文字符会被转换为 UTF-8 编码的百分号形式。 以下是使用 Java 实现 `urlEncode` 的示例代码: ```java import java.net.URLEncoder; import java.nio.charset.StandardCharsets; public class UrlEncoderExample { public static String urlEncode(String input) { return URLEncoder.encode(input, StandardCharsets.UTF_8); } public static void main(String[] args) { String inputStr = "Hello World! 欢迎来到编程世界"; String encodedStr = urlEncode(inputStr); System.out.println("Encoded String: " + encodedStr); } } ``` ### 解释 - `URLEncoder.encode` 是 Java 中用于 URL 编码的方法。 - 它会将输入字符串中的特殊字符转换为 URL 安全格式。 - `StandardCharsets.UTF_8` 指定了编码格式为 UTF-8。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值