diff --git a/lib/libPDF.php b/lib/libPDF.php
index <HASH>..<HASH> 100644
--- a/lib/libPDF.php
+++ b/lib/libPDF.php
@@ -222,7 +222,7 @@ class libPDF extends FPDF implements libPDFInterface {
 
 		$doc->loadXML("<root/>");
 
-		$html = preg_replace("/&(?![a-z\d]+|#\d+|#x[a-f\d]+);/i", "&amp;", $html);
+		$html = preg_replace("/&(?!([a-z\d]+|#\d+|#x[a-f\d]+);)/i", "&amp;", $html);
 
 		$f = $doc->createDocumentFragment();
 		if( !$f->appendXML($html) )
Fix HTMLText()'s entity detection not working all the time