Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 'plain_text' option + Added documenation for the $options parameter in the README. #122

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,37 @@ composer require html2text/html2text

## Basic Usage
```php
$html = new \Html2Text\Html2Text('Hello, &quot;<b>world</b>&quot;');
$html2text = new \Html2Text\Html2Text('Hello, &quot;<b>world</b>&quot;');

echo $html->getText(); // Hello, "WORLD"
echo $html2text->getText(); // Hello, "WORLD"
```

## Options

The constructor takes an optional 2nd parameter which is an array of options.
```php
$html2text = new \Html2Text\Html2Text(string $html, array $options = []);
```

Options and defaults are:
```php
[
'do_links' => 'inline', // 'none'
// 'inline' (show links inline)
// 'nextline' (show links on the next line)
// 'table' (if a table of link URLs should be listed after the text.
// 'bbcode' (show links as bbcode)

'width' => 70, // Maximum width of the formatted text, in columns.
// Set this value to 0 (or less) to ignore word wrapping
// and not constrain text to a fixed-width column.

'plain_text' => false, // If true then disables various pseudo formatting:
// No converting bold, th or headings to upper case.
// No character conversion to simulate strike through for <del>
// No adding _ around italic text (<i> <em> and <ins> tags)
// <hr> is replaced by "\n\n" rather than "\n-------------------------\n"
];
```

## History
Expand Down
58 changes: 55 additions & 3 deletions src/Html2Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,38 @@ class Html2Text
'[\\2]', // <img> with alt tag
);

/**
* List of pattern replacements corresponding to patterns searched.
* Used with 'plain_text' option.
*
* @var array $replace
* @see $search
*/
protected $replace_plain = array(
'', // Non-legal carriage return
' ', // Newlines and tabs
'', // <head>
'', // <script>s -- which strip_tags supposedly has problems with
'', // <style>s -- which strip_tags supposedly has problems with
'\\1', // <i>
'\\1', // <em>
'\\1', // <ins>
"\n\n", // <ul> and </ul>
"\n\n", // <ol> and </ol>
"\n\n", // <dl> and </dl>
"\t\\1\n", // <li> and </li>
" \\1\n", // <dd> and </dd>
"\t\\1", // <dt> and </dt>
"\n\t* ", // <li>
"\n\n", // <hr>
"<div>\n", // <div>
"\n\n", // <table> and </table>
"\n", // <tr> and </tr>
"\t\t\\1\n", // <td> and </td>
"", // <span class="_html2text_ignore">...</span>
'[\\2]', // <img> with alt tag
);

/**
* List of preg* regular expression patterns to search for,
* used in conjunction with $entReplace.
Expand Down Expand Up @@ -222,6 +254,13 @@ class Html2Text
'width' => 70, // Maximum width of the formatted text, in columns.
// Set this value to 0 (or less) to ignore word wrapping
// and not constrain text to a fixed-width column.

'plain_text' => false, // If true then disables various pseudo formatting:
// No converting bold, th or headings to upper case.
// No character conversion to simulate strike through for <del>
// No adding _ around italic text (<i> <em> and <ins> tags)
// No adding * for <li> or <dt>
// <hr> is replaced by "\n\n" rather than "\n-------------------------\n"
);

private function legacyConstruct($html = '', $fromFile = false, array $options = array())
Expand Down Expand Up @@ -371,7 +410,11 @@ protected function converter(&$text)
{
$this->convertBlockquotes($text);
$this->convertPre($text);
$text = preg_replace($this->search, $this->replace, $text);
if ($this->options['plain_text']) {
$text = preg_replace($this->search, $this->replace_plain, $text);
} else {
$text = preg_replace($this->search, $this->replace, $text);
}
$text = preg_replace_callback($this->callbackSearch, array($this, 'pregCallback'), $text);
$text = strip_tags($text);
$text = preg_replace($this->entSearch, $this->entReplace, $text);
Expand All @@ -385,12 +428,15 @@ protected function converter(&$text)
$text = str_replace('|+|amp|+|', '&', $text);

// Normalise empty lines
$text = preg_replace("/\n\s+\n/", "\n\n", $text);
$text = preg_replace("/[\n]{3,}/", "\n\n", $text);
$text = preg_replace("/\n(\xC2\xA0|\s)+\n/", "\n\n", $text);
$text = preg_replace("/\n{3,}/", "\n\n", $text);

// remove leading empty lines (can be produced by eg. P tag on the beginning)
$text = ltrim($text, "\n");

// remove trailing white space
$text = preg_replace("/[(\xC2\xA0|\s)]+$/", "", $text);

if ($this->options['width'] > 0) {
$text = wordwrap($text, $this->options['width']);
}
Expand Down Expand Up @@ -611,6 +657,9 @@ protected function pregPreCallback(/** @noinspection PhpUnusedParameterInspectio
*/
protected function toupper($str)
{
if ($this->options['plain_text']) {
return $str;
}
// string can contain HTML tags
$chunks = preg_split('/(<[^>]*>)/', $str, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

Expand Down Expand Up @@ -647,6 +696,9 @@ protected function strtoupper($str)
*/
protected function tostrike($str)
{
if ($this->options['plain_text']) {
return $str;
}
$rtn = '';
for ($i = 0; $i < mb_strlen($str); $i++) {
$chr = mb_substr($str, $i, 1);
Expand Down