src/Pyrite/Core/Filters.php
public static function html2text($html)
    {
        libxml_use_internal_errors(true);
        $doc = new \DOMDocument();

        // Hack to force UTF-8 processing on incomplete documents
        // From: http://php.net/manual/en/domdocument.loadhtml.php#95251
        $doc->loadHTML('<?xml encoding="UTF-8">' . $html, LIBXML_NOERROR | LIBXML_NOWARNING);
        foreach ($doc->childNodes as $item) {
            if ($item->nodeType == XML_PI_NODE) {
                $doc->removeChild($item);  // Remove the hack we inserted above
                break;  // We know there is only one
            };
        };
        $doc->encoding = 'UTF-8';  // Proper way to set encoding

        return self::_dom2text($doc);
    }
public static function html2text($html)
    {
        libxml_use_internal_errors(true);
        $doc = new \DOMDocument();

        // Hack to force UTF-8 processing on incomplete documents
        // From: http://php.net/manual/en/domdocument.loadhtml.php#95251
        $doc->loadHTML('<?xml encoding="UTF-8">' . $html, LIBXML_NOERROR | LIBXML_NOWARNING);
        foreach ($doc->childNodes as $item) {
            if ($item->nodeType == XML_PI_NODE) {
                $doc->removeChild($item);  // Remove the hack we inserted above
                break;  // We know there is only one
            };
        };
        $doc->encoding = 'UTF-8';  // Proper way to set encoding

        return self::_dom2text($doc);
    }
public static function html2text ( $ html ) { libxml_use_internal_errors ( true ) ; $ doc = new \ DOMDocument ( ) ; // Hack to force UTF-8 processing on incomplete documents // From: http://php.net/manual/en/domdocument.loadhtml.php#95251 $ doc -> loadHTML ( '<?xml encoding="UTF-8">' . $ html , LIBXML_NOERROR | LIBXML_NOWARNING ) ; foreach ( $ doc -> childNodes as $ item ) { if ( $ item -> nodeType == XML_PI_NODE ) { $ doc -> removeChild ( $ item ) ; // Remove the hack we inserted above break ; // We know there is only one } ; } ; $ doc -> encoding = 'UTF-8' ; // Proper way to set encoding return self :: _dom2text ( $ doc ) ; }
Convert an HTML string to a plain text approximation

This is a quick and dirty hack for the purpose of creating text/plain
alternatives to text/html E-mail messages.  There are a lot of edge
cases which are not handled well or at all.

@param string $html Source HTML document (with or without HTML/HEAD/BODY)

@return string
Convert an HTML string to a plain text approximation