Skip to content

Instantly share code, notes, and snippets.

@alash3al
Created February 20, 2015 21:22
Show Gist options
  • Save alash3al/e134f55f87a5cb27ef82 to your computer and use it in GitHub Desktop.
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()
/**
* 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