Wednesday, August 1, 2012

Remove parameter from URL PHP

If you are looking to remove a specific parameter from the URL here is how to do it:

Assume you have URL
http://www.b4blinky.com/index.php?option=com_content&view=article&id=1
And you want to remove the id


$currURL = $_SERVER[QUERY_STRING];
//this will get you option=com_content&view=article&id=1
parse_str($currURL,$params); //Puts the url parameters in key=>value pair
//This will give you option=>com_content, view=>article, id=>1
unset($params[$fieldName]);  //Removes the parameter on which 'x' is clicked
//This will remove the $fieldName from the array e.g. 'id'
$newURL = http_build_query($params); //Rebuilds the query with the remaining parameters
//This will rebuild array excluding id
$newURL = 'index.php?'.$newURL;
//and finally your newURL like
index.php?option=com_content&view=article

That you can modify if relative doesn't work for you.

Hope it helps. Happy coding :)

No comments:

Post a Comment