Last active
March 1, 2018 22:12
-
-
Save nhalstead/c3422e9f1fd213d381a12ad6e4664bf3 to your computer and use it in GitHub Desktop.
Phrasing URLs in php. From String to Array. From Array to String.
This file contains hidden or 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 | |
/** | |
* @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