我有一个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 /值中只获得电子邮件参数?

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


当前回答

在Laravel中,我使用:

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

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

其他回答

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

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

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

echo $_GET['email'];

将从url中提取电子邮件。

在Laravel中,我使用:

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

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

我从鲁埃尔的答案中创建了一个函数。

你可以用这个:

function get_valueFromStringUrl($url , $parameter_name)
{
    $parts = parse_url($url);
    if(isset($parts['query']))
    {
        parse_str($parts['query'], $query);
        if(isset($query[$parameter_name]))
        {
            return $query[$parameter_name];
        }
        else
        {
            return null;
        }
    }
    else
    {
        return null;
    }
}

例子:

$url = "https://example.com/test/the-page-here/1234?someurl=key&email=xyz5@test.com";
echo get_valueFromStringUrl($url , "email");

感谢@鲁埃尔。

$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