How to get parameters from a URL string?

前端 未结 13 1480
孤城傲影
孤城傲影 2020-11-22 04:05

I have a HTML form field $_POST[\"url\"] having some URL strings as the value. Example values are:

https://example.com/test/1234?email=xyz@test.com
         


        
13条回答
  •  鱼传尺愫
    2020-11-22 04:19

    I created function from @Ruel answer. You can use this:

    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;
        }
    }
    

    Example:

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

    Thanks to @Ruel

提交回复
热议问题