Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting HTML to plain text in PHP for e-mail

I use TinyMCE to allow minimal formatting of text within my site. From the HTML that's produced, I'd like to convert it to plain text for e-mail. I've been using a class called html2text, but it's really lacking in UTF-8 support, among other things. I do, however, like that it maps certain HTML tags to plain text formatting — like putting underscores around text that previously had <i> tags in the HTML.

Does anyone use a similar approach to converting HTML to plain text in PHP? And if so: Do you recommend any third-party classes that I can use? Or how do you best tackle this issue?

like image 928
Justin Stayton Avatar asked Dec 10 '09 22:12

Justin Stayton


People also ask

How do I convert an HTML email to plain text?

Change the message format for all messages you sendOn the File tab, choose Options > Mail. Under Compose messages, in the Compose messages in this format list, click HTML, Rich Text, or Plain Text.

How can I write HTML text in PHP?

The html_entity_decode() function converts HTML entities to characters. The html_entity_decode() function is the opposite of htmlentities().

How do I convert a Web page to plain text?

Click the “Save as” or “Save Page As” option and select “Text Files” from the Save as Type drop-down menu. Type a name for the text file and click “Save.” The text from the Web page will be extracted and saved as a text file that can be viewed in text editors and document programs such as Microsoft Word.


1 Answers

Use html2text (example HTML to text), licensed under the Eclipse Public License. It uses PHP's DOM methods to load from HTML, and then iterates over the resulting DOM to extract plain text. Usage:

// when installed using the Composer package $text = Html2Text\Html2Text::convert($html);  // usage when installed using html2text.php require('html2text.php'); $text = convert_html_to_text($html); 

Although incomplete, it is open source and contributions are welcome.

Issues with other conversion scripts:

  • Since html2text (GPL) is not EPL-compatible.
  • lkessler's link (attribution) is incompatible with most open source licenses.
like image 61
jevon Avatar answered Oct 05 '22 01:10

jevon