Looping through DOM to modify elements
To make it elegant clean code I would:


make proper use of blank lines;
abide by the coding standards defined by PHP-FIG;
use the DOMNodeList object as an array so I can use foreach();
be consistent with either single ' or double quotes ";
use a guard clause.


Refactored code:

$aEls = $doc-&gt;getElementsByTagName('a');

foreach ($aEls as $a) {
    $url = parse_url(strtolower($a-&gt;getAttribute('href')));

    if ($url['host'] === $localDomain) {
        continue;
    }

    $title = $a-&gt;getAttribute('title') . ' [external link to ' . $url['host'] . ']';

    $a-&gt;setAttribute('title', $title);
    $a-&gt;setAttribute('target', '_blank');
}


(not tested)