Answers for "get url info in php"

PHP
1

php get url infos

$url = 'http://username:password@hostname:9090/path?argument=value#fragment';
var_dump(parse_url($url, PHP_URL_SCHEME));   // http
var_dump(parse_url($url, PHP_URL_USER));     // username 
var_dump(parse_url($url, PHP_URL_PASS));     // password
var_dump(parse_url($url, PHP_URL_HOST));     // hostname
var_dump(parse_url($url, PHP_URL_PORT));     // 9090
var_dump(parse_url($url, PHP_URL_PATH));     // /path
var_dump(parse_url($url, PHP_URL_QUERY));    // argument=value
var_dump(parse_url($url, PHP_URL_FRAGMENT)); // fragment

// You can use "parse_str" to parse the URL Query to an Array
$query = parse_url($url, PHP_URL_QUERY);
$queryArray = array();
parse_str($query, $queryArray);
var_dump($queryArray);  // array('argument' => 'value')
Posted by: Guest on July-30-2021

Browse Popular Code Answers by Language