How to get parameters from a URL string?

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

提交回复
热议问题