How to remove http://, www and slash from URL in PHP?

$input="www.google.co.uk/";

// in case scheme relative URI is passed, e.g., //www.google.com/
$input = trim($input, "https://stackoverflow.com/");

// If scheme not included, prepend it
if (!preg_match('#^http(s)?://#', $input)) {
    $input="http://" . $input;
}

$urlParts = parse_url($input);

// remove www
$domain = preg_replace('/^www\./', '', $urlParts['host']);

echo $domain;

// output: google.co.uk

Works correctly with all your example inputs.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)