Skip to content

Instantly share code, notes, and snippets.

@muhfaris
Created December 22, 2017 03:17
Show Gist options
  • Save muhfaris/6e38a8b827964f0b21f1dd85edbae564 to your computer and use it in GitHub Desktop.
Save muhfaris/6e38a8b827964f0b21f1dd85edbae564 to your computer and use it in GitHub Desktop.
<?php
if(!function_exists("add_var_to_url")){
function add_var_to_url($variable_name,$variable_value,$url_string){
// first we will remove the var (if it exists)
// test if url has variables (contains "?")
if(strpos($url_string,"?")!==false){
$start_pos = strpos($url_string,"?");
$url_vars_strings = substr($url_string,$start_pos+1);
$names_and_values = explode("&",$url_vars_strings);
$url_string = substr($url_string,0,$start_pos);
foreach($names_and_values as $value){
list($var_name,$var_value)=explode("=",$value);
if($var_name != $variable_name){
if(strpos($url_string,"?")===false){
$url_string.= "?";
} else {
$url_string.= "&";
}
$url_string.= $var_name."=".$var_value;
}
}
}
// add variable name and variable value
if(strpos($url_string,"?")===false){
$url_string .= "?".$variable_name."=".$variable_value;
} else {
$url_string .= "&".$variable_name."=".$variable_value;
}
return $url_string;
}
}
add_var_to_url("total_items", 10, "http://example.com?sort_by=price");
or
add_var_to_url("total_items", 10, "http://example.com?total_items=5&sort_by=price");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment