Skip to content

Commit 81999a8

Browse files
committed
add option to hide alt text
1 parent 3b443cb commit 81999a8

2 files changed

Lines changed: 55 additions & 3 deletions

File tree

src/Html2Text.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ class Html2Text
6868
'/(<tr\b[^>]*>|<\/tr>)/i', // <tr> and </tr>
6969
'/<td\b[^>]*>(.*?)<\/td>/i', // <td> and </td>
7070
'/<span class="_html2text_ignore">.+?<\/span>/i', // <span class="_html2text_ignore">...</span>
71-
'/<(img)\b[^>]*alt=\"([^>"]+)\"[^>]*>/i', // <img> with alt tag
7271
);
7372

7473
/**
@@ -99,7 +98,6 @@ class Html2Text
9998
"\n", // <tr> and </tr>
10099
"\t\t\\1\n", // <td> and </td>
101100
"", // <span class="_html2text_ignore">...</span>
102-
'[\\2]', // <img> with alt tag
103101
);
104102

105103
/**
@@ -222,6 +220,9 @@ class Html2Text
222220
'width' => 70, // Maximum width of the formatted text, in columns.
223221
// Set this value to 0 (or less) to ignore word wrapping
224222
// and not constrain text to a fixed-width column.
223+
224+
'images'=>'alt', // 'alt' (show the alt text in brackets)
225+
// 'none' (don't show the alt text)
225226
);
226227

227228
private function legacyConstruct($html = '', $fromFile = false, array $options = array())
@@ -376,6 +377,7 @@ protected function doConvert()
376377

377378
protected function converter(&$text)
378379
{
380+
$this->setImgAltPreference();
379381
$this->convertBlockquotes($text);
380382
$this->convertPre($text);
381383
$text = preg_replace($this->search, $this->replace, $text);
@@ -406,6 +408,16 @@ protected function converter(&$text)
406408
}
407409
}
408410

411+
/**
412+
* show/hide alt text for images
413+
*/
414+
415+
protected function setImgAltPreference()
416+
{
417+
$this->search[] = '/<(img)\b[^>]*alt=\"([^>"]+)\"[^>]*>/i';
418+
$this->replace[] = $this->options['images'] == 'alt'?'[\\2]':'';
419+
}
420+
409421
/**
410422
* Helper function called by preg_replace() on link replacement.
411423
*

test/ImageTest.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,54 @@ public function imageDataProvider() {
3535
);
3636
}
3737

38+
public function ImageDataWithoutAltProvider() {
39+
return array(
40+
'Without alt tag' => array(
41+
'html' => '<img src="http://example.com/example.jpg">',
42+
'expected' => '',
43+
),
44+
'Without alt tag, wrapped in text' => array(
45+
'html' => 'xx<img src="http://example.com/example.jpg">xx',
46+
'expected' => 'xxxx',
47+
),
48+
'With alt tag' => array(
49+
'html' => '<img src="http://example.com/example.jpg" alt="An example image">',
50+
'expected' => '',
51+
),
52+
'With alt, and title tags' => array(
53+
'html' => '<img src="http://example.com/example.jpg" alt="An example image" title="Should be ignored">',
54+
'expected' => '',
55+
),
56+
'With alt tag, wrapped in text' => array(
57+
'html' => 'xx<img src="http://example.com/example.jpg" alt="An example image">xx',
58+
'expected' => 'xxxx',
59+
),
60+
'With italics' => array(
61+
'html' => '<img src="shrek.jpg" alt="the ogrelord" /> Blah <i>blah</i> blah',
62+
'expected' => ' Blah _blah_ blah'
63+
)
64+
);
65+
}
66+
3867
/**
3968
* @dataProvider imageDataProvider
4069
*/
41-
public function testImages($html, $expected)
70+
public function testImagesAlt($html, $expected)
4271
{
4372
$html2text = new Html2Text($html);
4473
$output = $html2text->getText();
4574

4675
$this->assertEquals($expected, $output);
4776
}
77+
78+
/**
79+
* @dataProvider ImageDataWithoutAltProvider
80+
*/
81+
public function testImagesNoAlt($html, $expected)
82+
{
83+
$html2text = new Html2Text($html,array('images' => 'none'));
84+
$output = $html2text->getText();
85+
86+
$this->assertEquals($expected, $output);
87+
}
4888
}

0 commit comments

Comments
 (0)