📜  PHP | rawurlencode()函数(1)

📅  最后修改于: 2023-12-03 14:45:18.485000             🧑  作者: Mango

PHP | rawurlencode()函数

介绍

rawurlencode()是PHP中的一个函数,用于将字符串进行URL编码,以符合URL中特殊字符的要求。

由于URL中只允许出现特定的字符,例如字母、数字、下划线、连字号以及点号,而其他字符(如空格、中文等)需要进行编码才能在URL中正确传输,否则可能会导致URL出现错误。

rawurlencode()函数将字符串中所有非字母和数字的字符都转化为URL编码的格式,使用百分号加上两位十六进制数(如 '%20' 表示空格),以便在URL中传输。

例如,对于字符串 "Hello World",使用 rawurlencode() 函数将返回 "Hello%20World"。

语法

rawurlencode(string $str):string

参数
  • $str:需要进行URL编码的字符串。
返回值
  • 返回URL编码后的字符串。
代码示例
// URL编码字符串
$str = "Hello World";
$urlStr = rawurlencode($str);
echo $urlStr; // 输出 "Hello%20World"
注意事项
  • 由于 rawurlencode() 函数只对字符串中的特殊字符进行编码,因此在进行URL拼接时,需要使用 urlencode() 函数对URL中的完整字符串进行编码,才能确保URL中不会出现非法字符。例如:
$url = 'http://example.com?q=' . urlencode('Hello World');
echo $url; // 输出 "http://example.com?q=Hello+World"