diff --git a/src/Html2Text.php b/src/Html2Text.php index 0329231..c065d07 100644 --- a/src/Html2Text.php +++ b/src/Html2Text.php @@ -116,6 +116,8 @@ public static function getDocument(string $html): DOMDocument $html = '
' . $html . ''; } + $html = (string) preg_replace('/&(?![a-z]+?;)/mi', '&', $html); + $load_result = $doc->loadHTML($html); if (! $load_result) { diff --git a/tests/Html2TextTest.php b/tests/Html2TextTest.php index a2a979e..ef4a3bc 100644 --- a/tests/Html2TextTest.php +++ b/tests/Html2TextTest.php @@ -14,12 +14,13 @@ $output = Html2Text::convert($input, $config); - expect($output)->toBe($expected); + expect(trim($output))->toBe(trim($expected)); })->with([ 'Basic' => 'basic', 'Anchor tags' => 'anchors', 'More anchor tags' => 'more-anchors', 'Break tag' => 'br', + 'Ampersand (&)' => 'ampersand', 'Break tags' => 'brs', 'Tables' => 'table', 'Non-breaking spaces (NBSP tag)' => 'nbsp', diff --git a/tests/fixtures/ampersand.html b/tests/fixtures/ampersand.html new file mode 100644 index 0000000..665e566 --- /dev/null +++ b/tests/fixtures/ampersand.html @@ -0,0 +1 @@ +& & diff --git a/tests/fixtures/ampersand.txt b/tests/fixtures/ampersand.txt new file mode 100644 index 0000000..09d480d --- /dev/null +++ b/tests/fixtures/ampersand.txt @@ -0,0 +1 @@ +& &