note 83693 deleted from function.urlencode by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 11:00:54 +0000 |
| Subject: | note 83693 deleted from function.urlencode by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197015@lists.php.net to get a copy of this message | ||
Note Submitter: muthuishere at gmail dot com
----
Most of us may need a function
if they have entire URL but you need to be encoding only the query values , not the URL and not
the parameters
The below function takes an URL as input and applies url encoding only to the parameter values
/******************************************
For eg www.google.com/?q=alka rani&start=100
urlencode
==> www.google.com/search?q=alka+rani&start=100
if you change it to rawurlencode
==> www.google.com/search?q=alka%20rani&start=100
**************************************/
function SmartUrlEncode($url){
//Extract the Querystr pos after ? mark
$startpos = stripos($url, "?");
//Extract the URl alone
$tmpurl=substr($url, 0 , $startpos+1) ;
//echo $tmpurl . "<br>";
//Extract the Querystr alone
$qryStr=substr($url, $startpos+1 ) ;
//echo $qryStr . "<br>";
//Split the querystring into & pairs
$qryvalues=explode("&", $qryStr);
foreach($qryvalues as &$value)
{
//Split the single data into two i.e data | value
$buffer=explode("=", $value);
// Urlencode only the value now , Change it to rawurlencode if necessary
$buffer[1]=urlencode($buffer[1]);
//Join values back in the array
$value = implode("=", $tmp);
}
//Output Querystr ,Join all the pairs with &
$finalqrystr=implode("&", $qryvalues);
// echo $finalqrystr . "<br>";
$finalURL=$tmpurl . $finalqrystr;
//echo $finalURL . "<br>";
return finalURL;
}
}