Created
February 20, 2015 21:22
-
-
Save alash3al/e134f55f87a5cb27ef82 to your computer and use it in GitHub Desktop.
Parse a string and split it into key value pairs, the php parse_str()
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
/** | |
* Parses the string into variables | |
* | |
* @param mixed $string | |
* @param mixed $equal | |
* @param mixed $separator | |
* | |
* @author Mohammed Al Ashaal <is.gd/alash3al> | |
* @version 1.0.0 | |
* | |
* @return Object | |
*/ | |
function parse_str($string, $equal, $separator) | |
{ | |
$equal = (typeof $equal == 'undefined') ? '=' : $equal; | |
$separator = (typeof $separator == 'undefined') ? '&' : $separator; | |
$ret = {}; | |
if ( $string.indexOf('?') === 0 ) | |
$string = $string.substr($string.indexOf('?') + 1); | |
$array = $string.split($separator); | |
for ( $i in $array ) { | |
$s = $array[$i].split($equal); | |
$ret[$s[0].trim()] = $s[1]; | |
} | |
return $ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment