diff --git a/src/Html2Text.php b/src/Html2Text.php index 6e0f9e5..5f65e04 100644 --- a/src/Html2Text.php +++ b/src/Html2Text.php @@ -360,6 +360,10 @@ protected function doConvert() $text = trim($this->html); } + // Inline data URIs (e.g. base64 images) never end up in the text, and very + // long ones exhaust the PCRE backtrack limit of the patterns below (#130) + $text = preg_replace('/(]*?\ssrc\s*=\s*)(["\'])data:[^"\']*+\2/i', '$1$2$2', $text); + $this->converter($text); if ($this->linkList) { diff --git a/test/DataUriTest.php b/test/DataUriTest.php new file mode 100644 index 0000000..9755e88 --- /dev/null +++ b/test/DataUriTest.php @@ -0,0 +1,40 @@ + array( + 'html' => '', + 'expected' => '* Item', + ), + 'Image in link' => array( + 'html' => ' text', + 'expected' => '[https://example.com] text', + ), + 'Image with alt tag in paragraph' => array( + 'html' => '

Hello logo world

', + 'expected' => 'Hello [logo] world', + ), + ); + } + + /** + * @dataProvider dataUriDataProvider + */ + public function testDataUri($html, $expected) + { + $html2text = new Html2Text($html); + $this->assertEquals($expected, trim($html2text->getText())); + } +}