Home > Article > Backend Development > How to convert url parameters into array in php
Conversion method: 1. Use "mb_substr($url,stripos($url,"?") 1)" to get the parameter part of the url; 2. Use "parse_str("parameter part",$arr) "Parse the parameters into variables and pass them into the specified array. The variable names are converted into key names and the variable values are converted into key values.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php will url Convert the parameters into an array
Implementation method:
Use stripos() to obtain the position of the character "?"
Use mb_substr() to intercept the string according to the obtained position and obtain the parameter part of the url
Use parse_str() to parse the parameters into variables, And pass in the specified array
Key points: parse_str()
parse_str() function parses the query string into a variable.
parse_str(string,array)
Parameters | Description |
---|---|
string | Required . Specifies the string to parse. |
array | Optional. Specifies the name of the array to store the variable. This parameter instructs the variable to be stored in an array. |
Implementation example:
<?php header('content-type:text/html;charset=utf-8'); $url="http://www.php.cn?title=我是小白&name=真的很白&text=但是决不放弃"; $str=mb_substr($url,stripos($url,"?")+1); echo $str."<br>"; parse_str($str, $arr); var_dump($arr); ?>
It can be seen that parse_str() Parse the parameters into variables one by one and pass them into the array; the variable names are converted into key names, and the variable values are converted into key values.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert url parameters into array in php. For more information, please follow other related articles on the PHP Chinese website!