Skip to content

Instantly share code, notes, and snippets.

@wpsmith
Created May 6, 2020 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpsmith/c38561320bcdeb01d0d95998e73a2f12 to your computer and use it in GitHub Desktop.
Save wpsmith/c38561320bcdeb01d0d95998e73a2f12 to your computer and use it in GitHub Desktop.
<?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