In PHP, both parse_str and mb_parse_str are functions used to parse URL-encoded query strings into variables. While they are functionally similar, there are some key differences between the two—especially when it comes to support for multibyte character sets (like Chinese or Japanese). This article takes a detailed look at the differences and appropriate use cases for each.
parse_str is a commonly used PHP function that parses a URL-encoded query string into PHP variables. Its syntax is as follows:
<span><span><span class="hljs-title function_ invoke__">parse_str</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$str</span></span><span>, </span><span><span class="hljs-keyword">array</span></span><span> &</span><span><span class="hljs-variable">$array</span></span><span> = </span><span><span class="hljs-literal">null</span></span><span>): </span><span><span class="hljs-keyword">void</span></span><span>
</span></span>
Parameters:
$str: The URL-encoded query string to be parsed.
$array (optional): If provided, the result will be stored as an associative array in this variable.
Return Value:
This function does not return a value. It modifies the $array parameter by reference or assigns global variables directly.
Example:
<span><span><span class="hljs-variable">$query</span></span><span> = </span><span><span class="hljs-string">"name=John&age=25&city=NewYork"</span></span><span>;
</span><span><span class="hljs-title function_ invoke__">parse_str</span></span><span>(</span><span><span class="hljs-variable">$query</span></span><span>, </span><span><span class="hljs-variable">$output</span></span><span>);
</span><span><span class="hljs-title function_ invoke__">print_r</span></span><span>(</span><span><span class="hljs-variable">$output</span></span><span>);
</span></span>
Output:
<span><span><span class="hljs-title function_ invoke__">Array</span></span><span>
(
[name] => John
[age] => </span><span><span class="hljs-number">25</span></span><span>
[city] => NewYork
)
</span></span>
mb_parse_str is a function provided by the multibyte string extension (mbstring). It is similar to parse_str, but is capable of correctly handling multibyte character sets (such as UTF-8 encoded Chinese characters). Its syntax closely resembles that of parse_str:
<span><span><span class="hljs-title function_ invoke__">mb_parse_str</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$str</span></span><span>, </span><span><span class="hljs-keyword">array</span></span><span> &</span><span><span class="hljs-variable">$array</span></span><span> = </span><span><span class="hljs-literal">null</span></span><span>): </span><span><span class="hljs-keyword">void</span></span><span>
</span></span>
Parameters:
$str: The URL-encoded query string, usually encoded in a multibyte character set.
$array (optional): The parsed result will be stored in this associative array.
Return Value:
Just like parse_str, mb_parse_str does not return a value. It outputs the result by reference through the $array parameter.
Example:
<span><span><span class="hljs-variable">$query</span></span><span> = </span><span><span class="hljs-string">"name=张三&age=25&city=北京"</span></span><span>;
</span><span><span class="hljs-title function_ invoke__">mb_parse_str</span></span><span>(</span><span><span class="hljs-variable">$query</span></span><span>, </span><span><span class="hljs-variable">$output</span></span><span>);
</span><span><span class="hljs-title function_ invoke__">print_r</span></span><span>(</span><span><span class="hljs-variable">$output</span></span><span>);
</span></span>
Output:
<span><span><span class="hljs-title function_ invoke__">Array</span></span><span>
(
[name] => 张三
[age] => </span><span><span class="hljs-number">25</span></span><span>
[city] => 北京
)
</span></span>
Multibyte Character Support:
parse_str is designed for single-byte character sets (like ISO-8859-1 or GBK). It may produce garbled output when parsing multibyte characters (e.g., Chinese, Japanese, Korean).
mb_parse_str, being part of the multibyte string extension, handles multibyte strings correctly. It's more reliable for query strings containing non-ASCII characters.
Character Set Handling:
parse_str does not process multibyte encodings. If the query string includes non-ASCII characters, the parsed result might be unreadable.
mb_parse_str supports encodings like UTF-8 and ensures characters are parsed accurately.
Performance:
parse_str is a built-in PHP function with better performance, suitable for simple, single-byte query strings.
mb_parse_str is part of the mbstring extension. If the extension isn't installed on the server, it can't be used. It may also be slightly slower due to extra processing of multibyte characters.
Use parse_str when:
You're working with query strings that contain only ASCII or single-byte characters. It's fast and straightforward, perfect for English-only or similar contexts.
Use mb_parse_str when:
The query strings include multibyte characters (like Chinese, Japanese, etc.). mb_parse_str is essential to ensure characters are parsed correctly without corruption.
parse_str is the standard function for parsing single-byte character set query strings. It's suitable for simple, ASCII-only scenarios.
mb_parse_str is provided by the multibyte string extension, specifically for handling query strings containing multibyte characters like Chinese or Japanese.
Choosing the right function based on your specific context ensures accurate and efficient parsing of query strings. If your application involves multilingual support or non-ASCII characters, using mb_parse_str is strongly recommended.