Created
May 6, 2020 16:34
-
-
Save wpsmith/c38561320bcdeb01d0d95998e73a2f12 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Parses a string into variables to be stored in an array. | |
* | |
* | |
* @param string $string The string to be parsed. | |
* @param array $array Variables will be stored in this array. | |
* | |
* @since 2.2.1 | |
* | |
*/ | |
function wp_parse_str( $string, &$array ) { | |
if ( '' == $string ) { | |
parse_str( $string, $array ); | |
/** This filter is documented below */ | |
$array = apply_filters( 'wp_parse_str', $array ); | |
return; | |
} | |
if ( null == $array ) { | |
$array = array(); | |
} | |
# split on outer delimiter | |
$pairs = explode( '&', $string ); | |
# loop through each pair | |
foreach ( (array) $pairs as $i ) { | |
# split into name and value | |
list( $name, $value ) = explode( '=', $i, 2 ); | |
# if name already exists | |
if ( isset( $array[ $name ] ) ) { | |
# stick multiple values into an array | |
if ( is_array( $array[ $name ] ) ) { | |
$array[ $name ][] = $value; | |
} else { | |
$array[ $name ] = array( $array[ $name ], $value ); | |
} | |
} # otherwise, simply stick it in a scalar | |
else { | |
$array[ $name ] = $value; | |
} | |
} | |
/** | |
* Filters the array of variables derived from a parsed string. | |
* | |
* @param array $array The array populated with variables. | |
* | |
* @since 2.3.0 | |
* | |
*/ | |
$array = apply_filters( 'wp_parse_str', $array ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment