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