4 * Class that handles operations involving percent-encoding in URIs.
6 class HTMLPurifier_PercentEncoder
10 * Fix up percent-encoding by decoding unreserved characters and normalizing
11 * @param $string String to normalize
13 function normalize($string) {
14 if ($string == '') return '';
15 $parts = explode('%', $string);
16 $ret = array_shift($parts);
17 foreach ($parts as $part) {
18 $length = strlen($part);
20 $ret .= '%25' . $part;
23 $encoding = substr($part, 0, 2);
24 $text = substr($part, 2);
25 if (!ctype_xdigit($encoding)) {
26 $ret .= '%25' . $part;
29 $int = hexdec($encoding);
31 ($int >= 48 && $int <= 57) ||
// digits
32 ($int >= 65 && $int <= 90) ||
// uppercase letters
33 ($int >= 97 && $int <= 122) ||
// lowercase letters
34 $int == 126 ||
$int == 45 ||
$int == 46 ||
$int == 95 // ~-._
36 $ret .= chr($int) . $text;
39 $encoding = strtoupper($encoding);
40 $ret .= '%' . $encoding . $text;