diff --git a/src/Html2Text.php b/src/Html2Text.php index 0dfb8d4..5ead1ff 100644 --- a/src/Html2Text.php +++ b/src/Html2Text.php @@ -67,6 +67,7 @@ class Html2Text '/(]*>|<\/tr>)/i', // and '/]*>(.*?)<\/td>/i', // and '/.+?<\/span>/i', // ... + '/<(img)[^>]*alt=\"([^>"]+)\"[^>]*>/i', // with alt tag ); /** @@ -97,7 +98,8 @@ class Html2Text "\n\n", // and
"\n", // and "\t\t\\1\n", // and - "" // ... + "", // ... + '[\\2]', // with alt tag ); /** diff --git a/test/ImageTest.php b/test/ImageTest.php new file mode 100644 index 0000000..04a3aa6 --- /dev/null +++ b/test/ImageTest.php @@ -0,0 +1,42 @@ + array( + 'html' => '', + 'expected' => '', + ), + 'Without alt tag, wrapped in text' => array( + 'html' => 'xxxx', + 'expected' => 'xxxx', + ), + 'With alt tag' => array( + 'html' => 'An example image', + 'expected' => '[An example image]', + ), + 'With alt, and title tags' => array( + 'html' => 'An example image', + 'expected' => '[An example image]', + ), + 'With alt tag, wrapped in text' => array( + 'html' => 'xxAn example imagexx', + 'expected' => 'xx[An example image]xx', + ), + ); + } + + /** + * @dataProvider testImageDataProvider + */ + public function testImages($html, $expected) + { + $html2text = new Html2Text($html); + $output = $html2text->getText(); + + $this->assertEquals($expected, $output); + } +}