Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/Html2Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ class Html2Text {

public static function defaultOptions() {
return array(
'ignore_errors' => false,
'drop_links' => false,
'ignore_errors' => false,
'drop_links' => false,
'keep_empty_lines' => false,
);
}

Expand Down Expand Up @@ -59,7 +60,7 @@ public static function convert($html, $options = array()) {
$output = static::iterateOverNode($doc, null, false, $is_office_document, $options);

// process output for whitespace/newlines
$output = static::processWhitespaceNewlines($output);
$output = static::processWhitespaceNewlines($output, $options['keep_empty_lines']);

return $output;
}
Expand Down Expand Up @@ -99,9 +100,10 @@ static function zwnjCodes() {
* Remove leading or trailing spaces and excess empty lines from provided multiline text
*
* @param string $text multiline text any number of leading or trailing spaces or excess lines
* @param boolean $remove_empty_lines remove unnecessary empty lines or not
* @return string the fixed text
*/
static function processWhitespaceNewlines($text) {
static function processWhitespaceNewlines($text, $keep_empty_lines = false) {

// remove excess spaces around tabs
$text = preg_replace("/ *\t */im", "\t", $text);
Expand All @@ -127,7 +129,9 @@ static function processWhitespaceNewlines($text) {
$text = static::fixNewLines($text);

// remove unnecessary empty lines
$text = preg_replace("/\n\n\n*/im", "\n\n", $text);
if (!$keep_empty_lines) {
$text = preg_replace("/\n\n\n*/im", "\n\n", $text);
}

return $text;
}
Expand Down Expand Up @@ -482,7 +486,7 @@ static function iterateOverNode($node, $prevName = null, $in_pre = false, $is_of

case "blockquote":
// process quoted text for whitespace/newlines
$output = static::processWhitespaceNewlines($output);
$output = static::processWhitespaceNewlines($output, $options['keep_empty_lines']);

// add leading newline
$output = "\n" . $output;
Expand Down