'What is the best php library to convert html to plain text from email?

I have been testing 2 different PHP libraries (html2text & htmlpurifier) to convert html email to plain text. The problem is that none of them are formatting the EOL correctly. The resulting plain text is often in the same paragraph.

Here is the function I am using with htmlpurifier :

function RemoveAllHTMLCode($html) {
    require_once "PATH/htmlpurifier/library/HTMLPurifier.auto.php";
    $config = HTMLPurifier_Config::createDefault();
    $config->set('HTML.Allowed', '');
    $purifier = new HTMLPurifier($config);
    return $purifier->purify($html);
}

Is there a work around? Or another way to do it?



Solution 1:[1]

Maybe try running nl2br before putting it through HTML Purifier?

Solution 2:[2]

In case it helps anyone else, we just picked up soundasleep/html2text for this, and so far so good!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Edward Z. Yang
Solution 2 Harry Lewis