1URI::Escape(3) User Contributed Perl Documentation URI::Escape(3)
2
3
4
6 URI::Escape - Escape and unescape unsafe characters
7
9 use URI::Escape;
10 $safe = uri_escape("10% is enough\n");
11 $verysafe = uri_escape("foo", "\0-\377");
12 $str = uri_unescape($safe);
13
15 This module provides functions to escape and unescape URI strings as
16 defined by RFC 3986.
17
18 A URI consists of a restricted set of characters. The restricted set
19 of characters consists of digits, letters, and a few graphic symbols
20 chosen from those common to most of the character encodings and input
21 facilities available to Internet users. They are made up of the
22 "unreserved" and "reserved" character sets as defined in RFC 3986.
23
24 unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
25 reserved = ":" / "/" / "?" / "#" / "[" / "]" / "@"
26 "!" / "$" / "&" / "'" / "(" / ")"
27 / "*" / "+" / "," / ";" / "="
28
29 In addition, any byte (octet) can be represented in a URI by an escape
30 sequence: a triplet consisting of the character "%" followed by two
31 hexadecimal digits. A byte can also be represented directly by a
32 character, using the US-ASCII character for that octet.
33
34 Some of the characters are reserved for use as delimiters or as part of
35 certain URI components. These must be escaped if they are to be
36 treated as ordinary data. Read RFC 3986 for further details.
37
38 The functions provided (and exported by default) from this module are:
39
40 uri_escape( $string )
41 uri_escape( $string, $unsafe )
42 Replaces each unsafe character in the $string with the
43 corresponding escape sequence and returns the result. The $string
44 argument should be a string of bytes. The uri_escape() function
45 will croak if given a characters with code above 255. Use
46 uri_escape_utf8() if you know you have such chars or/and want chars
47 in the 128 .. 255 range treated as UTF-8.
48
49 The uri_escape() function takes an optional second argument that
50 overrides the set of characters that are to be escaped. The set is
51 specified as a string that can be used in a regular expression
52 character class (between [ ]). E.g.:
53
54 "\x00-\x1f\x7f-\xff" # all control and hi-bit characters
55 "a-z" # all lower case characters
56 "^A-Za-z" # everything not a letter
57
58 The default set of characters to be escaped is all those which are
59 not part of the "unreserved" character class shown above as well as
60 the reserved characters. I.e. the default is:
61
62 "^A-Za-z0-9\-\._~"
63
64 uri_escape_utf8( $string )
65 uri_escape_utf8( $string, $unsafe )
66 Works like uri_escape(), but will encode chars as UTF-8 before
67 escaping them. This makes this function able do deal with
68 characters with code above 255 in $string. Note that chars in the
69 128 .. 255 range will be escaped differently by this function
70 compared to what uri_escape() would. For chars in the 0 .. 127
71 range there is no difference.
72
73 The call:
74
75 $uri = uri_escape_utf8($string);
76
77 will be the same as:
78
79 use Encode qw(encode);
80 $uri = uri_escape(encode("UTF-8", $string));
81
82 but will even work for perl-5.6 for chars in the 128 .. 255 range.
83
84 Note: JavaScript has a function called escape() that produces the
85 sequence "%uXXXX" for chars in the 256 .. 65535 range. This
86 function has really nothing to do with URI escaping but some folks
87 got confused since it "does the right thing" in the 0 .. 255 range.
88 Because of this you sometimes see "URIs" with these kind of
89 escapes. The JavaScript encodeURIComponent() function is similar
90 to uri_escape_utf8().
91
92 uri_unescape($string,...)
93 Returns a string with each %XX sequence replaced with the actual
94 byte (octet).
95
96 This does the same as:
97
98 $string =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
99
100 but does not modify the string in-place as this RE would. Using
101 the uri_unescape() function instead of the RE might make the code
102 look cleaner and is a few characters less to type.
103
104 In a simple benchmark test I did, calling the function (instead of
105 the inline RE above) if a few chars were unescaped was something
106 like 40% slower, and something like 700% slower if none were. If
107 you are going to unescape a lot of times it might be a good idea to
108 inline the RE.
109
110 If the uri_unescape() function is passed multiple strings, then
111 each one is returned unescaped.
112
113 The module can also export the %escapes hash, which contains the
114 mapping from all 256 bytes to the corresponding escape codes. Lookup
115 in this hash is faster than evaluating "sprintf("%%%02X", ord($byte))"
116 each time.
117
119 URI
120
122 Copyright 1995-2004 Gisle Aas.
123
124 This program is free software; you can redistribute it and/or modify it
125 under the same terms as Perl itself.
126
127
128
129perl v5.12.0 2010-03-14 URI::Escape(3)