How to Get Domain Name And Components from URL in PHP

The parse_url() function is inbuilt of PHP. you use parse_url() to get domain name and part, parse_url() function returned associative array There will be at least one element within the array. In this list, possible keys are: scheme-host, port, user, transfer, route, query-after the question mark?

Syntax:

parse_url( $url, $component = -1 )

Example 1

Below examples pass single parameters the use of parse_url() function in PHP:


$url = 'http://www.w3-html.com/path?id=30';
var_dump(parse_url($url));

//Output
array(4) {
  ["scheme"]=> 
  string(4) "http" 
  ["host"]=>
  string(15) "www.w3-html.com"
  ["path"]=>
  string(5) "/path"
  ["query"]=>
  string(17) "id=10"
}

Example 2

Below examples pass two parameters the use of parse_url() function in PHP:


$url = 'http://www.w3-html.com/path?id=30';
var_dump(parse_url($url,PHP_URL_HOST));

//Output
 string(15) "www.w3-html.com"

Note: In above example first parameters is URL and second parameters to retrieve a specific URL in the form of string( PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or PHP_URL_FRAGMENT )

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments