Home > php教程 > php手册 > PHP用一句代码把字符串转换为数组的方式

PHP用一句代码把字符串转换为数组的方式

PHPz
Release: 2018-09-30 14:43:27
forward
1462 people have browsed it

如果你是想把url里的参数解析到数组可以直接用 parse_str 

不用这么复杂  用内置函数即可  建议好好看看文档

parse_str('id=123&title=标题&type=news&content=内容',$parameters);
print_r($parameters);
 
Array
(
    [id] => 123
    [title] => 标题
    [type] => news
    [content] => 内容
)
Copy after login

如果是一个完整的url可以用 parse_url

print_r(parse_url('http:// zhidao.baidu.com:88/?id=123&title=标题&type=news&content=内容'));
 
Array
(
    [scheme] => http
    [host] => zhidao.baidu.com
    [port] => 88
    [path] => /
    [query] => id=123&title=标题&type=news&content=内容
)
Copy after login

然后对 query使用 parse_str即可

更多相关教程请访问 php编程从入门到精通全套视频教程

Related labels:
source:csdn.net
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template