Skip to content

Instantly share code, notes, and snippets.

@nhalstead
Last active March 1, 2018 22:12
Show Gist options
  • Save nhalstead/c3422e9f1fd213d381a12ad6e4664bf3 to your computer and use it in GitHub Desktop.
Save nhalstead/c3422e9f1fd213d381a12ad6e4664bf3 to your computer and use it in GitHub Desktop.
Phrasing URLs in php. From String to Array. From Array to String.
<?php
/**
* @link https://gist.github.com/nhalstead/c3422e9f1fd213d381a12ad6e4664bf3
*/
/**
* Process URL
* @param String URL Input
* @return Array The URL data that is Given.
*/
function process_url($url){
$url_parts = parse_url($url);
if(!isset($url_parts['qu'.'ery'])){
$url_parts['qu'.'ery'] = "";
}
parse_str($url_parts['qu'.'ery'], $url_parts['qu'.'ery']); // Decode the Query Data string to Array.
return $url_parts;
}
/**
* Make URL
* @param Array The Array of the URL Data
* @return String THe URL made from the URL Data Input.
*/
function make_url($dataURI){
$scheme = isset($dataURI['scheme'])?$dataURI['scheme']:"";
if($scheme == ""){ $scheme = "//"; }
else { $scheme = $scheme."://"; }
$host = isset($dataURI['host'])?$dataURI['host']:"localhost";
$path = isset($dataURI['path'])?$dataURI['path']:"/";
$query = isset($dataURI['query'])?$dataURI['query']:array();
$query = http_build_query($query); // Encode the Query Array to the String
if($query != ""){ $query = "?".$query; }
$r = $scheme.$host.$path.$query;
return $r;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment