0 follower

Final Class Yiisoft\Mailer\HtmlToTextBodyConverter

InheritanceYiisoft\Mailer\HtmlToTextBodyConverter

Utility for converting HTML body to text body.

Public Methods

Hide inherited methods

Method Description Defined By
__invoke() Yiisoft\Mailer\HtmlToTextBodyConverter
convert() Generates a text body from an HTML body. Yiisoft\Mailer\HtmlToTextBodyConverter

Method Details

Hide inherited methods

__invoke() public method

public string __invoke ( string $html )
$html string

                public function __invoke(string $html): string
{
    return self::convert($html);
}

            
convert() public static method

Generates a text body from an HTML body.

public static string convert ( string $html )
$html string

The HTML body.

return string

The text body.

                public static function convert(string $html): string
{
    if (preg_match('~<body[^>]*>(.*?)</body>~is', $html, $match)) {
        $html = $match[1];
    }
    /**
     * Remove style and script
     *
     * @var string $html We use valid regular expression, so `preg_replace()` will return string.
     */
    $html = preg_replace('~<((style|script))[^>]*>(.*?)</\1>~is', '', $html);
    // strip all HTML tags and decode HTML entities
    $text = html_entity_decode(strip_tags($html), ENT_QUOTES | ENT_HTML5);
    /**
     * Improve whitespace
     *
     * @var string $text We use valid regular expression, so `preg_replace()` will return string.
     */
    $text = preg_replace("~^[ \t]+~m", '', trim($text));
    /**
     * @var string We use valid regular expression, so `preg_replace()` will return string.
     */
    return preg_replace('~\R\R+~mu', "\n\n", $text);
}