note 95785 deleted from function.rawurlencode by danbrown
| From: | danbrown@php.net | Date: | Thu, 21 Jan 2010 22:40:29 +0000 |
| Subject: | note 95785 deleted from function.rawurlencode by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-165181@lists.php.net to get a copy of this message | ||
Note Submitter: Skyrock
----
Here's a slightly improved version of the above code.
An incorrect URI like http://www.skyrock.com/1234/foo&bar.html
gets rewritten as: http://www.skyrock.com/1234/foo%26bar.html
function _cb_clean_uri_path_component($prx) {
$component = $prx[1];
return '/' . rawurlencode($component);
}
function uri_clean($uri) {
$matches = NULL;
if (empty($uri)) {
return $uri;
}
$ta = parse_url($uri);
if (empty($ta)) {
return $uri;
}
$scheme = $host = $path = $query = $fragment = '';
if (!empty($ta['scheme'])) {
$scheme = $ta['scheme'] . '://';
}
if (!empty($ta['host'])) {
$host = $ta['host'];
}
if (!empty($ta['path'])) {
$path = $ta['path'];
}
if (!empty($ta['query'])) {
$query = '?' . $ta['query'];
}
if (!empty($ta['fragment'])) {
$fragment = '#' . $ta['fragment'];
}
$fixed_path = preg_replace_callback('£/([^/?]+)£Dus',
'_cb_clean_uri_path_component', $path);
$clean_uri = $scheme . $host . $fixed_path . $query . $fragment;
return $clean_uri;
}