Parsing domain from a URL

To parse the domain from a URL in PHP, you can use the parse_url function, which will return an associative array containing information about the URL. You can then extract the host element from the array, which will contain the domain of the URL.

Watch a course Learn object oriented PHP

Here is an example of how you can parse the domain from a URL in PHP:

<?php

$url = 'http://www.example.com/path/to/page.html';
$url_parts = parse_url($url);
$domain = $url_parts['host'];

// Output: "www.example.com"
echo $domain;

Note that this will not work for URLs that do not have a host, such as data: or javascript: URLs.