<a href="URL">XXX</a>XXX<a href="URL">XXX</a>XXX<a href="URL"><img src="URL" alt=""></a><a href="URL">XXX</a>XXX.
<a href="URL" target="_blank"><img src="URL" alt=""></a>
<a .*?>(<img.*?/?>)</a>
~<a[^>]+>(?=<img)~
$text = '<a href="URL1">XXX</a>XXX<a href="URL2">XXX</a>XXX<a href="URL3"><img src="URL" alt=""></a><a href="URL">';
$regexp = '~<a[^>]+>(?=<img)~';
preg_match($regexp, $text, $regs);
echo htmlspecialchars($regs[0]);
<a href="URL3">