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?


parse_url( $url, $component = -1 )

Example 1

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

$url = '';

array(4) {
  string(4) "http" 
  string(15) ""
  string(5) "/path"
  string(17) "id=10"

Example 2

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

$url = '';

 string(15) ""

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
Notify of
Inline Feedbacks
View all comments