PHP中的urlencode,rawurlencode和JS中的encodeURI,encodeURIComponent


PHP中的urlencode,rawurlencode和JS中的encodeURI,encodeURIComponent

【PHP中的urlencode和rawurlencode】
urlencode之前有看過其源碼實現PHP 源碼閱讀筆記二十三 :urlencode函數
二都的區別僅在” “空格上,rawurlencode()會把空格編碼為%20,而urlencode會把空格編碼為+

【JS中的encodeURI和encodeURIComponent】
encodeURI 方法不會對下列字符進行編碼:”:”、”/”、”;” 和 “?”,而encodeURIComponent會編碼這些字符

【urlencode與encodeURI】
首先,我們看下這4種編碼方式針對ASCII的127個字符編碼后的差別,顯示代碼如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
/** * 生成urlencode,rawurlencode,encodeURI,encodeURIComponent的編碼結果 2010-10-29 sz * @author phppan.p#gmail.com http://www.phppan.com * 哥學社成員(http://www.blog-brother.com/) * @package test */ header("Content-type:text/html;charset=utf-8");   echo <<<STYLE <style type="text/css"> table { cursor:default; font-family:Verdana,Helvetica,sans-serif; font-size:8pt; } td { background:none repeat scroll 0 0 #EFEFEF; text-align:center; width:100px; } </style> STYLE; echo '<table >'; echo _tr(_td("ASCII") . _td("urlenocde") . _td("rawurlencode") . _td("encodeURI") . _td("encodeURIComponent")); for ($i = 0; $i < 128; $i++) { $ch = chr($i); $td = _td($ch) . _td(urlencode($ch)) . _td(rawurlencode($ch)); $td .= _td(_encodeURI($ch)) . _td(_encodeURIComponent($ch));   echo _tr($td); } echo "</table>";

對比urlencode和encodeURI的不同,可以看到#$&+,/:;=?@這些符號編碼結果不同,
於是對於需要在PHP中編碼后,給js的encodeURI使用的操作可以使用如下函數:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
/** * urlencode適用於js版本 2010-10-29 sz * @author phppan.p#gmail.com http://www.phppan.com * 哥學社成員(http://www.blog-brother.com/) * @package test */ header("Content-type:text/html;charset=utf-8");   function urlencode_js($str) { $str_len = strlen($str);   $new = array(); for ($i = 0; $i < $str_len; $i++) { $ch = $str[$i]; if (strpos("#$&+,/:;=?@", $ch) !== FALSE) { $new[] = $ch; } else { $new[] = urlencode($ch); } }   return implode("", $new); }   $encode_str = urlencode_js("a漢bc中文 章+aa#$&+,/:;=?@a漢bc中文 章+aa");   echo <<<HTML <script type="text/javascript"> document.write(decodeURI("$encode_str") + "<br />"); </script> HTML; die();

【urlencode和urldecode的PHP實現】(折騰一個重復輪子玩)
以下代碼純屬折騰,如有雷同,不勝榮幸

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
 
<?php
/** * urlencode和urldecode的PHP版本 2010-10-29 sz * @author phppan.p#gmail.com http://www.phppan.com * 哥學社成員(http://www.blog-brother.com/) * @package test */ header("Content-type:text/html;charset=utf-8");   $str = "a漢bc中文 章+aa:/;?()'!-.*_~";   /** * urlencode的PHP實現 * 純屬折騰 其C實現請參照PHP源碼 url.c文件中php_url_encode函數 * @param <type> $str * @return <type> */ function myurlencode($str) { $len = strlen($str);   $rs = array(); for ($i = 0; $i < $len; $i++) { $ch = $str[$i]; if ($ch == ' ') { $rs[] = '+'; } else if (!encodecheck($ch)) { $rs[] = strtoupper('%' . dechex(ord($ch) >> 4) . dechex(ord($ch) & 15)); } else { $rs[] = $ch; } }   return implode("", $rs); }   /** * 判斷是否為字符和字線以及_-. * 相當於c中的!isalnum(c) && strchr("_-.", c) == NULL(PHP源碼) * @param <type> $ch * @return <type> */ function encodecheck($ch) { $pattern = "/[a-zA-Z0-9_\-\.]/"; return preg_match($pattern, strval($ch)); }   /** * 判斷是否為16進制數 * @param <type> $ch * @return <type> */ function checkhex($ch) { $hexstr = "0123456789ABCDEF"; return strpos($hexstr, strval($ch)) === FALSE ? FALSE : TRUE; }   /** * urldecode的PHP實現 * 純屬折騰 * @param <type> $str * @return <type> */ function myurldecode($str) { $len = strlen($str);   $rs = array(); for ($i = 0; $i < $len; $i++) { $ch = $str[$i]; if ($ch == '+') { $rs[] = ' '; } else if ($ch == '%' && isset($str[$i + 1]) && checkhex($str[$i + 1]) && isset($str[$i + 2]) && checkhex($str[$i + 2])) { $rs[] = chr(hexdec($str[$i + 1] . $str[$i + 2])); $i += 2; } else { $rs[] = $ch; } }   return implode("", $rs); }   /* 測試 */ echo $str, '<br />'; echo urldecode(myurlencode($str)), '<br />'; echo myurldecode(urlencode($str)), '<br />'; die();

以上算是對urlencode和urldecode實現的一次復習吧。

–EOF–


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM