browser lang:en
getLink() is a function that will make creating URLs much easier. This function expects two arguments - the first argument is the URL and the second one is an array with all the parameters that should be passed with the URL. The function will combine the existing URL with the parameter array and return the resulting URL.
One important thing to note is that the final URL will use '&' to separate the parameters instead of just '&'. This is to ensure valid HTML in the output. If you don't like that, just str_replace() it out.
If there is extra parameters in the first URL, that will be present in the final URL.
Arguments
First Argument ($url)
The base URL. The parameters will be added to this.
Second Argument ($params)
An associative array containing all the parameters and their values.
Third Argument ($use_existing_arguments)
This is an optional argument. If this is set to 'true', the function will use the parameters that are present in the current page along with the $params array when creating the URL.
Example Code
Two Arguments
getLink("http://www.google.com/search",array(
"q"=>"binny",
"hello"=>"world",
"results"=>10)
);
getLink("http://www.example.com/edit.php",array(
"item_id" => 15,
"return_url"=> 'index.php'
),
true);
/**
* Create a link by joining the given URL and the parameters given as the second argument.
* Arguments : $url - The base url.
* $params - An array containing all the parameters and their values.
* $use_existing_arguments - Use the parameters that are present in the current page
* Return : The new url.
* Example :
* getLink("http://www.google.com/search",array("q"=>"binny","hello"=>"world","results"=>10));
* will return
* http://www.google.com/search?q=binny&hello=world&results=10
*/
function getLink($url,$params=array(),$use_existing_arguments=false) {
if($use_existing_arguments) $params = $params + $_GET;
if(!$params) return $url;
$link = $url;
if(strpos($link,'?') === false) $link .= '?'; //If there is no '?' add one at the end
elseif(!preg_match('/(\?|\&(amp;)?)$/',$link)) $link .= '&'; //If there is no '&' at the END, add one.
$params_arr = array();
foreach($params as $key=>$value) {
if(gettype($value) == 'array') { //Handle array data properly
foreach($value as $val) {
$params_arr[] = $key . '[]=' . urlencode($val);
}
} else {
$params_arr[] = $key . '=' . urlencode($value);
}
}
$link .= implode('&',$params_arr);
return $link;
}
PHP is one of the most widely used open-source server-side scripting languages that exist today. With over…
in:Scripts and tutorials (0 comments)In a FBML Facebook App, your quick jump menu will require a little tweak to work in FBJS…
in:Scripts and tutorials (0 comments)Every single day, someone, somewhere is discussing something important to your business; your brand, your executives, your…
in:Scripts and tutorials (0 comments)The Singleton Pattern is one of the GoF (Gang of Four) Patterns. This particular pattern provides a…
in:Scripts and tutorials (0 comments)Because of the near-spherical shape of the Earth, calculating an accurate distance between two points requires the use…
in:Scripts and tutorials (1 comments)With this script we can limit the download speed // local file that should be send to the client $local_file…
in:Scripts and tutorials (0 comments)Some hosts disabled the ini setting allow_url_fopen. This also means that the ability to easily grab images…
in:Scripts and tutorials (0 comments)The act to verify if a file exists, is one of more important tasks related to files operations,…
in:Scripts and tutorials (0 comments)
