Skip to content

Instantly share code, notes, and snippets.

@dinocajic
Last active August 29, 2015 14:27
Show Gist options
  • Save dinocajic/5c03f487fbf3060afa85 to your computer and use it in GitHub Desktop.
Save dinocajic/5c03f487fbf3060afa85 to your computer and use it in GitHub Desktop.
Converts URI to Array
<?php
class Frequent {
/**
* String query to associative array
*
* Grabs the URI string from the address bar
* Since all of my pages start with a ?page=__something__, I split the uri string into 2 segments and at that point
* and discard the http://example.com (i.e. $uri[0])
* Split the remainder of into it's own segments using the & sign as the delimiter
* Initialize the $segments array and assign the "page" key to it (again, since ?page=__something__ will always be the first portion of the URI string
* Remove the page value from the $uri array since we don't need it any more
*
* Loop through the remainder of the $uri values and split them using the = sign as the delimiter. Store into $temp variable
* - i.e. $uri will look like this at this point:
* array("type=2",
* "id=10",
* "xxx=something"
* );
*
* Assign $temp[0] as the key to $segments array and $temp[1] as value to $segments array
* - i.e. $temp will look like this:
* array("type" => "2",
* "id" => "10",
* "xxx" => "something"
* );
*
* The loop will also take care of any long strings that might have been created
* - i.e. ?page=__something__&type=2&id=10&xxx=something&xxx=something2&xxx=something3
* - In this example, each xxx key will get overwritten with the last xxx
*
* Any additional segments are appended last: that is if an array of key => value pairs is passed
* - i.e. $add_segment = array("final" => "3");
* In this example the final $segments array will look like this:
* array("type" => "2",
* "id" => "10",
* "xxx" => "something"
* "final" => "3"
* );
*
* Also, since $add_segments is passed last, if the same key name is passed as one already assigned to segments,
* the already assigned key will get overwritten.
*
* @param $add_segment | Must be either NULL or Array
* @return array $segments;
*/
public function uriToArray($add_segment = NULL) {
$uri = $_SERVER["REQUEST_URI"];
$uri = explode("?page=", $uri);
unset($uri[0]);
$uri = explode("&", $uri[1]);
$segments["page"] = $uri[0];
unset($uri[0]);
foreach($uri as $segment) {
$temp = explode("=", $segment);
$segments[$temp[0]] = $temp[1];
}
if ($add_segment !== NULL) {
foreach($add_segment as $key => $value) {
$segments[$key] = $value;
}
}
return $segments;
}
/**
* Generates a link
*
* Grabs the segments from the uriToArray() method and passes any additional segments via $add_segment
* - i.e. $this->_frequent_obj->generateLink(array("type" => "2"));
* - If no additional segments, NULL will be passed
* Since ?page=__something__ is always the first GET variable, the $link string is initialized with the ?page=XXX
* The page key/value pair is unset from the $link_array variable since we don't want to pass it again
* The rest of the key => value pairs are run added to the $link string variable via &key=value
* The link is returned after all values have been concatenated
*
* @param array $add_segment
* @return string $link
*/
public function generateLink($add_segment = NULL) {
$link_array = $this->uriToArray($add_segment);
$link = "?page=" . $link_array["page"];
unset($link_array["page"]);
foreach($link_array as $key => $value) {
$link .= "&" . $key . "=" . $value;
}
return $link;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment