<?php
$_SERVER['HTTP_HOST'] = "ideone.com"; // fake it, we don't need it!
$output = '<a href="https://google.com">External Link</a> | <a href="#anchor">Anchor</a> | <a href="http://i...content-available-to-author-only...e.com">Internal Link</a>';

// start
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;

$doc->loadHTML($output);

$links = $doc->getElementsByTagName('a');
foreach ($links as $item) {
	$href = parse_url($item->getAttribute('href'), PHP_URL_HOST);
	if (!empty($href) && $href != $_SERVER['HTTP_HOST']){
		$item->setAttribute('target','_blank');
	}
}


$output = $doc->saveHTML();


echo($output);