wp_parse_str()
Parses the query string, for example, foo=1&foo2=2 and creates an array of data from it.
Uses the PHP function parse_str() to parse the string.
The function does not return anything but fills the second parameter $array with data. The resulting array is processed by the function stripslashes_deep() - removes escaping slashes.
add_query_arg() is the reverse function that assembles the array into query parameters.
Used By: wp_parse_args()
1 time — 0.000001 sec (speed of light) | 50000 times — 0.07 sec (speed of light) | PHP 7.3.12, WP 5.3.2
Hooks from the function
Returns
null. The function modifies the passed second parameter, i.e., the result can be found in the variable $array.
Usage
wp_parse_str( $input_string, $result );
- $string(string) (required)
- The string to be parsed
- $array(array) (required)
- The variable where the obtained data will be placed. Passed by reference.
Examples
#1 Demo
Let's break down the query string into array of key => value pairs:
$string = '?one=1&foo=some&two=2&bool=true'; wp_parse_str( $string, $array ); print_r( $array ); /* Will withdraw: Array ( [?one] => 1 [foo] => some [two] => 2 [bool] => true ) */
Changelog
| Since 2.2.1 | Introduced. |
wp_parse_str() wp parse str code WP 6.9.1
function wp_parse_str( $input_string, &$result ) {
parse_str( (string) $input_string, $result );
/**
* Filters the array of variables derived from a parsed string.
*
* @since 2.2.1
*
* @param array $result The array populated with variables.
*/
$result = apply_filters( 'wp_parse_str', $result );
}