Looks like all you really need to do there is store the domains you find in an array, and then sort it.
<?php
$string = file_get_contents('text.txt');
$pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,3})(?:\.[a-z]{2})?/i';
$result = preg_match_all($pattern, $string, $matches);
$domains = [];
if($result) {
foreach(array_unique($matches[0]) as $email) {
$domain = strstr($email, '@', false);
$domain1 = str_replace('@', '', $domain);
// echo $domain1 . '<br />';
$domains[$domain1] = $domain1;
}
asort($domains);
foreach($domains as $domain) {
echo $domain . '<br />';
}
}
Alternatively, you could probably change that regex to match only the domain parts of the email addresses and skip a step.