@@ -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 *
0 commit comments