Source for file PercentEncoder.php
Documentation is available at PercentEncoder.php
* Class that handles operations involving percent-encoding in URIs.
* Be careful when reusing instances of PercentEncoder. The object
* you use for normalize() SHOULD NOT be used for encode(), or
* Reserved characters to preserve when using encode().
* String of characters that should be preserved while using encode().
// unreserved letters, ought to const-ify
for ($i =
48; $i <=
57; $i++
) $this->preserve[$i] =
true; // digits
for ($i =
65; $i <=
90; $i++
) $this->preserve[$i] =
true; // upper-case
for ($i =
97; $i <=
122; $i++
) $this->preserve[$i] =
true; // lower-case
$this->preserve[95] =
true; // Underscore _
// extra letters not to escape
if ($preserve !==
false) {
for ($i =
0, $c =
strlen($preserve); $i <
$c; $i++
) {
* Our replacement for urlencode, it encodes all non-reserved characters,
* as well as any extra characters that were instructed to be preserved.
* Assumes that the string has already been normalized, making any
* and all percent escape sequences valid. Percents will not be
* re-escaped, regardless of their status in $preserve
* @param $string String to be encoded
* @return Encoded string.
public function encode($string) {
for ($i =
0, $c =
strlen($string); $i <
$c; $i++
) {
if ($string[$i] !==
'%' &&
!isset
($this->preserve[$int =
ord($string[$i])]) ) {
$ret .=
'%' .
sprintf('%02X', $int);
* Fix up percent-encoding by decoding unreserved characters and normalizing.
* @warning This function is affected by $preserve, even though the
* usual desired behavior is for this not to preserve those
* characters. Be careful when reusing instances of PercentEncoder!
* @param $string String to normalize
if ($string ==
'') return '';
foreach ($parts as $part) {
$encoding =
substr($part, 0, 2);
$ret .=
chr($int) .
$text;
$ret .=
'%' .
$encoding .
$text;
Documentation generated on Thu, 19 Jun 2008 18:49:52 -0400 by phpDocumentor 1.4.2