@@ -451,7 +451,7 @@ public function __toString()
451
451
452
452
453
453
/**
454
- * Similar to rawurldecode, but preserve reserved chars encoded.
454
+ * Similar to rawurldecode, but preserves reserved chars encoded.
455
455
* @param string to decode
456
456
* @param string reserved characters
457
457
* @return string
@@ -461,14 +461,15 @@ public static function unescape($s, $reserved = '%;/?:@&=+$,')
461
461
// reserved (@see RFC 2396) = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ","
462
462
// within a path segment, the characters "/", ";", "=", "?" are reserved
463
463
// within a query component, the characters ";", "/", "?", ":", "@", "&", "=", "+", ",", "$" are reserved.
464
- preg_match_all ('#(?<=%)[a-f0-9][a-f0-9]#i ' , $ s , $ matches , PREG_OFFSET_CAPTURE | PREG_SET_ORDER );
465
- foreach (array_reverse ($ matches ) as $ match ) {
466
- $ ch = chr (hexdec ($ match [0 ][0 ]));
467
- if (strpos ($ reserved , $ ch ) === FALSE ) {
468
- $ s = substr_replace ($ s , $ ch , $ match [0 ][1 ] - 1 , 3 );
469
- }
464
+ if ($ reserved === '' ) {
465
+ return rawurldecode ($ s );
466
+ }
467
+ $ pattern = '#((?:%(?: ' . implode ('| ' , str_split (bin2hex ($ reserved ), 2 )) . '))++)#i ' ;
468
+ $ parts = preg_split ($ pattern , $ s , -1 , PREG_SPLIT_DELIM_CAPTURE );
469
+ for ($ i = 0 ; $ i < count ($ parts ); $ i += 2 ) {
470
+ $ parts [$ i ] = rawurldecode ($ parts [$ i ]);
470
471
}
471
- return $ s ;
472
+ return implode ( '' , $ parts ) ;
472
473
}
473
474
474
475
}
0 commit comments