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

How to convert url parameters into array in php

青灯夜游
青灯夜游Original
2022-04-21 20:50:403876browse

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)
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(&#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);
?>

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!

Statement:
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