- var_dump: documentation ( source)
- array_reverse: documentation ( source)
- explode: documentation ( source)
- array_pop: documentation ( source)
<?php
$emailDomain = 'subdomain.example.co.uk';
$domainParts = explode('.', $emailDomain);
// The TLD will never be in the allow list, so we can pop it off first
$domain = array_pop($domainParts);
$listToCheck = [];
foreach (array_reverse($domainParts) as $domainPart) {
$domain = $domainPart .'.'. $domain;
$listToCheck[] = $domain;
}
var_dump($listToCheck);