How to get parameters from a URL string?

前端 未结 13 1470
孤城傲影
孤城傲影 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

    0 讨论(0)
  • 2020-11-22 04:23

    you can use below code to get email address after ? in the URL

    <?php
    if (isset($_GET['email'])) {
        echo $_GET['email'];
    }

    0 讨论(0)
  • 2020-11-22 04:24

    In Laravel, I'm use:

    private function getValueFromString(string $string, string $key)
    {
        parse_str(parse_url($string, PHP_URL_QUERY), $result);
    
        return isset($result[$key]) ? $result[$key] : null;
    }
    
    0 讨论(0)
  • 2020-11-22 04:25

    Use $_GET['email'] for parameters in URL. Use $_POST['email'] for posted data to script. Or use _$REQUEST for both. Also, as mentioned, you can use parse_url() function that returns all parts of URL. Use a part called 'query' - there you can find your email parameter. More info: http://php.net/manual/en/function.parse-url.php

    0 讨论(0)
  • 2020-11-22 04:27

    A much more secure answer that I'm surprised is not mentioned here yet:

    filter_input

    So in the case of the question you can use this to get an email value from the URL get parameters:

    $email = filter_input( INPUT_GET, 'email', FILTER_SANITIZE_EMAIL );

    For other types of variables, you would want to choose a different/appropriate filter such as FILTER_SANITIZE_STRING.

    I suppose this answer does more than exactly what the question asks for - getting the raw data from the URL parameter. But this is a one-line shortcut that is the same result as this:

    $email = $_GET['email'];
    $email = filter_var( $email, FILTER_SANITIZE_EMAIL );
    

    Might as well get into the habit of grabbing variables this way.

    0 讨论(0)
  • 2020-11-22 04:28

    Dynamic function which parse string url and get value of query parameter passed in URL

    function getParamFromUrl($url,$paramName){
      parse_str(parse_url($url,PHP_URL_QUERY),$op);// fetch query parameters from string and convert to associative array
      return array_key_exists($paramName,$op) ? $op[$paramName] : "Not Found"; // check key is exist in this array
    }
    

    Call Function to get result

    echo getParamFromUrl('https://google.co.in?name=james&surname=bond','surname'); // bond will be o/p here
    
    0 讨论(0)
提交回复
热议问题