When working with PHP, you may need to parse a query string into an array. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
mb_parse_str: This function decodes query-string variables into a destination array, applying mbstring-related encoding settings.
What Is mb_parse_str in PHP?
Parse a query string into an array. This function decodes query-string variables into a destination array, applying mbstring-related encoding settings.
Syntax:
mb_parse_str(string $string, array &$result): bool
Parameters and return value: The signature above identifies the arguments and return type where the API has a single call. For topic guides, follow the sequence of calls in the examples. Always distinguish a returned value from an object or variable modified by reference.
Important: Do not treat parsed query parameters as trusted or validated. An array argument receives the parsed result by reference.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
mb_parse_str("name=Alex&city=Paris", $fields);
print_r($fields);
The code focuses on the main operation. This function decodes query-string variables into a destination array, applying mbstring-related encoding settings.
Example 2: Another Case
Now compare a different input, locale, date, or option to see another part of the behavior.
<?php
mb_parse_str("items[]=pen&items[]=book", $fields);
print_r($fields);
What changes? Do not treat parsed query parameters as trusted or validated. An array argument receives the parsed result by reference.
Practical Example: Applying It in an Application
Use the following separate example as a starting point for a small application task. Check any extension or environment requirements below before running it.
<?php
$query = "page=2&sort=recent";
mb_parse_str($query, $parameters);
$page = filter_var($parameters["page"] ?? 1, FILTER_VALIDATE_INT);
echo "Page: " . ($page ?: 1);
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. This function decodes query-string variables into a destination array, applying mbstring-related encoding settings.
Important Behavior and Edge Cases
- Core behavior: This function decodes query-string variables into a destination array, applying mbstring-related encoding settings.
- Inputs and results: Do not treat parsed query parameters as trusted or validated. An array argument receives the parsed result by reference.
- Alternatives: parse_str() parses query strings without the mbstring-specific conversion behavior.
- Compatibility: mbstring is required for mb_* functions.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
mb_parse_str | Parse a query string into an array. |
| Related PHP approach | parse_str() parses query strings without the mbstring-specific conversion behavior. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | Do not treat parsed query parameters as trusted or validated. An array argument receives the parsed result by reference. |
| Confusing this API with a related operation | parse_str() parses query strings without the mbstring-specific conversion behavior. |
| Assuming output is identical across environments | Check return values and required extensions or versions. mbstring is required for mb_* functions. |
Key Takeaways
- mb_parse_str helps you parse a query string into an array.
- This function decodes query-string variables into a destination array, applying mbstring-related encoding settings.
- Do not treat parsed query parameters as trusted or validated. An array argument receives the parsed result by reference.
Use mb_parse_str when you need to parse a query string into an array. Start with the first example, compare the second, and adapt the practical example to your inputs. Confirm the return value and environment requirements before adding it to production code.