Passing variables through the URL is awesome but how do you update, say, the id or the page when there's twenty different variables (index.php?id=2325213&page=30&view=210&sort=213&user=231221). Here's a quick function to create a new URL string that will replace values of specific variables. You can also remove variables which are not necessary.
First of all though you'll need this function here. This function will spit out the page name for you. If you are using htaccess rewrite for .htm then replace index.php with index.htm:
//Filtering Page
$get_page = substr($page_url, strripos($page_url, '/')+1);
//Position of The ?
if (strpos('.' . $get_page, '?') == 1) {
$get_page = 'index.htm' . $get_page;
}
//Exempt Query String
if ($ex_qs==1) {
if (@strpos($get_page, '?')) $get_page = substr($get_page, 0, strpos($get_page, '?'));
//They Want Only Query String
} elseif ($ex_qs==2) {
//There is A Query String
if (@strpos($get_page, '?')) {
$currentQS = substr($get_page, strpos($get_page, '?')+1, strlen($get_page));
} else {
$currentQS = ";
}
$get_page = $currentQS;
}
return $get_page;
}
Now for the function to replace variables:
//Get Current Page
$gl_current_page = getPage($_SERVER['REQUEST_URI']);
$gl_current_page_nqs = getPage($_SERVER['REQUEST_URI'], 1);
$gl_current_ext = getPage($_SERVER['REQUEST_URI'], 2);
//If There's Extention in the Current Page
if ($gl_current_ext!=") {
//Return Extension
$returnExt = $gl_current_page;
$currentExt = '?' . $gl_current_ext;
//ADDING VAR *****************************************
if ($variables!=") {
//If There Are Multiple Variables
if (@strpos($variables, "&")) {
//Explode The String
$explodeVar = explode("&", $variables);
} else {
$explodeVar = array($variables);
}
//Check if Variables Changed
foreach ($explodeVar as $currentVar) {
//Not Empty
if ($currentVar!=") {
//Explode The Var
$currentExplode = explode("=", $currentVar);
//Find In Current String
if (preg_match('/[\?|\&]'.$currentExplode[0].'=/', $currentExt) > 0) {
//Replace
$currentExt = preg_replace('/'.$currentExplode[0].'=[^\&]*/', $currentVar, $currentExt);
//Not In String Add to it
} else {
$currentExt .= '&' . $currentVar;
}
}
}
}
//Subtracting Var *****************************************
if ($subtract_variables != NULL) {
//If There Are Multiple Variables
if (@strpos($subtract_variables, "&")) {
//Explode The String
$explodeVar = explode("&", $subtract_variables);
} else {
$explodeVar = array($subtract_variables);
}
//Subtracting Them
foreach ($explodeVar as $currentVar) {
//Explode The Var
$currentExplode = explode("=", $currentVar);
//Find In Current String
if (preg_match('/(&|\?)'.$currentExplode[0].'=/', $currentExt) > 0) $currentExt = preg_replace('/(&|\?)'.$currentExplode[0].'=([^&|$]+)/', "$1", $currentExt);
}
//Removing Duplicate
$currentExt = preg_replace('/\?&/', '?', preg_replace('/&&/', '&', $currentExt));
}
//Build Return Extention
$returnExt = $gl_current_page_nqs . $currentExt;
//No Extention
} else {
$returnExt = $gl_current_page_nqs . '?'. $variables;
}
//Returning
return $returnExt;
}
To use the function just use the following examples:
<a href="<?php varURL('id=5&name=Berry'); ?>">Link</a>
//Remove the id variables – just set the =1 the one will automatically be replaced anything
<a href="<?php varURL(", id=1); ?>">Link</a>
//Replace the id with 5 and remove the name variable
<a href="<?php varURL('id=5', 'name=Berry'); ?>">Link</a>
Tags: Function, PHP Script, Variable













H there :) Thanks for the nice update :) Looking forward to reading from your blog :)
UPDATE: For those who have used this function before this date please note I have updated the core function of this script. After some further testing I've noticed some cases didn't work. I apologize for that.