note 86378 deleted from function.parse-url by cmb
| From: | cmb@php.net | Date: | Sun, 06 Oct 2019 08:17:12 +0000 |
| Subject: | note 86378 deleted from function.parse-url by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212506@lists.php.net to get a copy of this message | ||
Note Submitter: gautam at rogers dot com
----
What about using something like this to safely encoding all the values that are passed in the query
portion?
Example input:
http://www.example.com/?first=john&last=smith&email=john@smith.com
Result:
http://www.example.com/?first=john&last=smith&email=john%40smith.com
<?php
function safe_url($url) {
// Make sure we have a string to work with
if(!empty($url)) {
// Explode into URL keys
$urllist=parse_url($url);
// Make sure we have a valid result set and a query field
if(is_array($urllist) && isset($urllist["query"])) {
// Explode into key/value array
$keyvalue_list=explode("&",($urllist["query"]));
// Store resulting key/value pairs
$keyvalue_result=array();
foreach($keyvalue_list as $key=>$value) {
// Explode each individual key/value into an array
$keyvalue=explode("=",$value);
// Make sure we have a "key=value" array
if(count($keyvalue)==2) {
// Encode the value portion
$keyvalue[1]=urlencode($keyvalue[1]);
// Add our key and encoded value into the result
array_push($keyvalue_result,implode("=",$keyvalue));
}
}
// Repopulate our query key with encoded results
$urllist["query"]=implode("&",$keyvalue_result);
// Build the the final output URL
$url=(isset($urllist["scheme"])?$urllist["scheme"]."://":"").
(isset($urllist["user"])?$urllist["user"].":":"").
(isset($urllist["pass"])?$urllist["pass"]."@":"").
(isset($urllist["host"])?$urllist["host"]:"").
(isset($urllist["port"])?":".$urllist["port"]:"").
(isset($urllist["path"])?$urllist["path"]:"").
(isset($urllist["query"])?"?".$urllist["query"]:"").
(isset($urllist["fragment"])?"#".$urllist["fragment"]:"");
}
}
return $url;
}
?>