function mailsystem_html_to_text in Mail System 6.2
Same name and namespace in other branches
- 8.2 html_to_text.inc \mailsystem_html_to_text()
- 7.3 html_to_text.inc \mailsystem_html_to_text()
- 7.2 html_to_text.inc \mailsystem_html_to_text()
Transform an HTML string into plain text, preserving the structure of the markup. Useful for preparing the body of a node to be sent by e-mail.
The output will be suitable for use as 'format=flowed; delsp=yes' text (RFC 3676) and can be passed directly to drupal_mail() for sending.
We deliberately use variable_get('mail_line_endings', MAIL_LINE_ENDINGS) rather than "\r\n".
This function provides suitable alternatives for the following tags:
<a> <address> <b> <blockquote> <br /> <caption> <cite> <dd> <div> <dl> <dt> <em> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <ol> <p> <pre> <strong> <table> <tbody> <td> <tfoot> <thead> <tr> <u> <ul>
The following tag attributes are supported:
- <a href=...>: Hyperlink destination urls.
- <li value=...>: Ordered list item numbers.
- <ol start=...>: Ordered list start number.
Parameters
$string: The string to be transformed.
$allowed_tags: (optional) If supplied, a list of tags that will be transformed. If omitted, all supported tags are transformed.
Return value
The transformed string.
See also
1 call to mailsystem_html_to_text()
- DefaultMailSystem::format in ./
mailsystem.module - Concatenate and wrap the e-mail body for plain-text mails.
1 string reference to 'mailsystem_html_to_text'
- mailsystem_init in ./
mailsystem.module - Implements hook_init().
File
- ./
html_to_text.inc, line 103 - Copy of drupal_html_to_text improvements from issue #299138.
Code
function mailsystem_html_to_text($string, $allowed_tags = NULL) {
$eol = variable_get('mail_line_endings', MAIL_LINE_ENDINGS);
// Cache list of supported tags.
static $supported_tags;
if (!isset($supported_tags)) {
$supported_tags = array(
'a',
'address',
'b',
'blockquote',
'br',
'cite',
'dd',
'div',
'dl',
'dt',
'em',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'hr',
'i',
'li',
'ol',
'p',
'pre',
'strong',
'table',
'td',
'tr',
'u',
'ul',
);
}
// Make sure only supported tags are kept.
$allowed_tags = isset($allowed_tags) ? array_intersect($supported_tags, $allowed_tags) : $supported_tags;
// Parse $string into a DOM tree.
$dom = filter_dom_load($string);
$notes = array();
// Recursively convert the DOM tree into plain text.
$text = _mailsystem_html_to_text($dom->documentElement, $allowed_tags, $notes);
// Hard-wrap at 1000 characters (including the line break sequence)
// and space-stuff special lines.
$text = mailsystem_wrap_mail($text, array(
'max' => 1000 - strlen($eol),
'hard' => TRUE,
));
// Change non-breaking spaces back to regular spaces, and trim line breaks.
// chr(160) is the non-breaking space character.
$text = str_replace(chr(160), ' ', trim($text, $eol));
// Add footnotes;
if ($notes) {
// Add a blank line before the footnote list.
$text .= $eol;
foreach ($notes as $url => $note) {
$text .= $eol . '[' . $note . '] ' . $url;
}
}
return $text;
}