note 40352 deleted from function.parse-url by didou
| From: | didou@php.net | Date: | Sat, 03 Feb 2007 03:09:42 +0000 |
| Subject: | note 40352 deleted from function.parse-url by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-122620@lists.php.net to get a copy of this message | ||
Note Submitter: osfist at yahoo dot com
----
Functions to edit a url query key and its value.
You can also add a new query key and its value.
I gathered and modified useful functions for my code.
It works fine as you see at the test result.
<?php
function get_query_edited_url($url, $arg, $val) {
$parsed_url = parse_url($url);
parse_str($parsed_url['query'],$url_query);
$url_query[$arg] = $val;
$parsed_url['query'] = http_implode($url_query);
$url = glue_url($parsed_url);
return $url;
}
function http_implode($arrayInput) {
if (! is_array($arrayInput))
return false;
$url_query="";
foreach ($arrayInput as $key=>$value) {
$url_query .=(strlen($url_query)>1)?'&':"";
$url_query .= urlencode($key).'='.urlencode($value);
}
return $url_query;
}
function glue_url($parsed) {
if (! is_array($parsed))
return false;
$url = $parsed['scheme'] ? $parsed['scheme'].':'
.((strtolower($parsed['scheme']) == 'mailto') ?
'':'//'): '';
$url .= $parsed['user'] ? $parsed['user']
.($parsed['pass']?
':'.$parsed['pass']:'').'@':'';
$url .= $parsed['host'] ? $parsed['host'] : '';
$url .= $parsed['port'] ? ':'.$parsed['port'] : '';
$url .= $parsed['path'] ? $parsed['path'] : '';
$url .= $parsed['query'] ? '?'.$parsed['query'] : '';
$url .= $parsed['fragment'] ? '#'.$parsed['fragment'] :
'';
return $url;
}
/* Test & Output
$url =
"http://user:pass@host/path?arg1=value&arg2=myval2#anchor";
print "<br>0:".$url;
print "<br>1:".get_query_edited_url($url,'arg1','CHANGED');
//Arg1
print "<br>2:".get_query_edited_url($url,'arg2','CHANGED');
//Arg2
print "<br>3:".get_query_edited_url($url,'arg3','NEW'); //New
Argument
*/
// Output
//http://user:pass@host/path?arg=value&arg2=myval2#anchor
//http://user:pass@host/path?arg=value&arg2=CHANGED#anchor
//http://user:pass@host/path?arg=CHANGED&arg2=myval2#anchor
?>