How to get the domain from an email address or a link
Short answer
For a link use parse_url():
<?php
echo parse_url('https://shop.example.com/catalog?page=2', PHP_URL_HOST);Output:
shop.example.comThe domain from an email address
Take everything after the last @ sign:
<?php
$email = 'anna@mail.example.com';
echo substr($email, strrpos($email, '@') + 1);Output:
mail.example.comWithout www
The prefix is removed separately:
<?php
$host = parse_url('https://www.example.com/about', PHP_URL_HOST);
echo preg_replace('/^www\./', '', $host);Output:
example.com