如何从 URL 字符串中获取参数? [英] How can I get parameters from a URL string?

查看:36
本文介绍了如何从 URL 字符串中获取参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 HTML 表单字段 $_POST["url"],有一些 URL 字符串作为值.

示例值为:

<前>https://example.com/test/1234?email=xyz@test.comhttps://example.com/test/1234?basic=2&email=xyz2@test.comhttps://example.com/test/1234?email=xyz3@test.comhttps://example.com/test/1234?email=xyz4@test.com&testin=123https://example.com/test/the-page-here/1234?someurl=key&email=xyz5@test.com

如何才能从这些 URL/值中只获取 email 参数?

请注意,我不是从浏览器地址栏中获取这些字符串的.

解决方案

您可以使用 parse_url()parse_str() 为此.

$parts = parse_url($url);parse_str($parts['query'], $query);回声 $query['email'];

如果你想用PHP动态获取$url,看看这个问题:

获取 PHP 中的完整 URL

I have an HTML form field $_POST["url"], having some URL strings as the value.

Example values are:

https://example.com/test/1234?email=xyz@test.com
https://example.com/test/1234?basic=2&email=xyz2@test.com
https://example.com/test/1234?email=xyz3@test.com
https://example.com/test/1234?email=xyz4@test.com&testin=123
https://example.com/test/the-page-here/1234?someurl=key&email=xyz5@test.com

etc.

How can I get only the email parameter from these URLs/values?

Please note that I am not getting these strings from the browser address bar.

解决方案

You can use the parse_url() and parse_str() for that.

$parts = parse_url($url);
parse_str($parts['query'], $query);
echo $query['email'];

If you want to get the $url dynamically with PHP, take a look at this question:

Get the full URL in PHP

这篇关于如何从 URL 字符串中获取参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆