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

举例如下:

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.

如何从这些url /值中只获得电子邮件参数?

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


当前回答

$web_url = 'http://www.writephponline.com?name=shubham&email=singh@gmail.com';
$query = parse_url($web_url, PHP_URL_QUERY);
parse_str($query, $queryArray);

echo "Name: " . $queryArray['name'];  // Result: shubham
echo "EMail: " . $queryArray['email']; // Result:singh@gmail.com

其他回答

$uri = $_SERVER["REQUEST_URI"];
$uriArray = explode('/', $uri);
$page_url = $uriArray[1];
$page_url2 = $uriArray[2];
echo $page_url; <- See the value

这对我来说使用PHP非常有用。

你可以使用下面的代码来获取邮件地址之后?在URL:

< ?php 如果(isset($ _gets[‘电子邮件’]) echo $ _GET[邮件’]; 的

之后的所有参数?可以使用$_GET数组访问。所以,

echo $_GET['email'];

将从url中提取电子邮件。

使用parse_url()和parse_str()方法。parse_url()将把URL字符串解析为其各部分的关联数组。由于您只需要URL的单个部分,因此可以使用快捷方式返回包含所需部分的字符串值。接下来,parse_str()将为查询字符串中的每个参数创建变量。我不喜欢破坏当前上下文,因此提供第二个参数将所有变量放入一个关联数组中。

$url = "https://mysite.com/test/1234?email=xyz4@test.com&testin=123";
$query_str = parse_url($url, PHP_URL_QUERY);
parse_str($query_str, $query_params);
print_r($query_params);

//Output: Array ( [email] => xyz4@test.com [testin] => 123 ) 

在Laravel中,我使用:

private function getValueFromString(string $string, string $key)
{
    parse_str(parse_url($string, PHP_URL_QUERY), $result);

    return isset($result[$key]) ? $result[$key] : null;
}