php get hostname from url code example

Example 1: php get domain from url

$url = 'http://google.com/dhasjkdas/sadsdds/sdda/sdads.html';
$parse = parse_url($url);
echo $parse['host']; // prints 'google.com'

Example 2: php get url path name

parse_url( $_SERVER[ 'REQUEST_URI' ], PHP_URL_PATH );

Example 3: php get hostname

<?php
echo gethostname(); // may output e.g,: sandie

// Or, an option that also works before PHP 5.3
echo php_uname('n'); // may output e.g,: sandie
?>

Example 4: part of url php

//https://www.google.com/search?key=1234
	$url = $_SERVER['REQUEST_URI']; 
    $url_components = parse_url($url); 
    parse_str($url_components['query'], $params); 
    $key = $params['key']; 

// key=1234

Example 5: php get domain name with https

echo $_SERVER['SERVER_NAME']; //Outputs www.example.com

Tags:

Php Example