Created
January 6, 2013 13:11
-
-
Save geeksunny/4467037 to your computer and use it in GitHub Desktop.
A simple class for handling URL strings for links.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class urlstring { | |
private $params = array(); | |
function __construct() { | |
// Pulling parameters from the URL string. | |
$str = explode("&",$_SERVER['QUERY_STRING']); | |
foreach ($str as $item) { | |
$split = explode("=",$item); | |
$this->params[$split[0]] = $split[1]; | |
} | |
} | |
/* ~~~~~~~~~~~~~~~ | |
~~~ Setters ~~~ | |
~~~~~~~~~~~~~~~ */ | |
// Set a single parameter & value. | |
public function set_parameter($name, $value) { | |
$this->params[$name] = $value; | |
} | |
// Set multiple parameters / values at a time. | |
// $data should be an associative array. | |
public function set_parameters($data = array()) { | |
if (is_array($data)) { | |
foreach ($data as $name=>$value) { | |
$this->params[$name] = $value; | |
} | |
} | |
} | |
/* ~~~~~~~~~~~~~~~ | |
~~~ Getters ~~~ | |
~~~~~~~~~~~~~~~ */ | |
// Returns a URL string with all string parameters. | |
public function get_string($prefix = false) { | |
$str = ""; | |
foreach ($this->params as $name=>$value) { | |
$str .= $name."=".$value."&"; | |
} | |
$str = rtrim($str,"&"); | |
if ($prefix) { | |
$str = "?".$str; | |
} | |
return $str; | |
} | |
// Returns a URL string with all string parameters and a one-time modified data set. | |
// $data should be an associative array. | |
public function get_modded_string($data = array(), $prefix = false) { | |
$params = $this->params; | |
if (is_array($data)) { | |
foreach ($data as $name=>$value) { | |
$params[$name] = $value; | |
} | |
} | |
$str = ""; | |
foreach ($params as $name=>$value) { | |
$str .= $name."=".$value."&"; | |
} | |
$str = rtrim($str,"&"); | |
if ($prefix) { | |
$str = "?".$str; | |
} | |
return $str; | |
} | |
/* ~~~~~~~~~~~~~~~ | |
~~~ Helpers ~~~ | |
~~~~~~~~~~~~~~~ */ | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment