Get domain name without www if url contain www

To get the domain name without the ‘www’ prefix in PHP, you can use the parse_url function to extract the host from the URL and then remove the ‘www’ prefix if it exists. Here’s an example:

<?php
$url = "http://www.example.com/path/to/page";

// Extract the host from the URL
$host = parse_url($url, PHP_URL_HOST);

// Remove 'www' if it exists
$domainWithoutWWW = preg_replace('/^www\./', '', $host);

echo $domainWithoutWWW;
?>