Home > Backend Development > PHP Problem > How to convert url parameters into array in php

How to convert url parameters into array in php

青灯夜游
Release: 2023-03-15 19:02:01
Original
4173 people have browsed it

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.

How to convert url parameters into array in php

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)
Copy after login
ParametersDescription
stringRequired . 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(&#39;content-type:text/html;charset=utf-8&#39;);   
$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);
?>
Copy after login

How to convert url parameters into array in php

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!

Related labels:
php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template