imanghafoori1/laravel-widgetize

View on GitHub
src/Utils/HtmlMinifier.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace Imanghafoori\Widgets\Utils;

/**
 * Class Html Minifier.
 */
class HtmlMinifier
{
    private $replace = [
        '/<!--[\s\S]*?-->/' => '', //remove comments
        "/<\?php/" => '<?php ',
        "/\n([\S])/" => '$1',
        "/\r/" => '', // remove carriage return
        "/\n/" => '', // remove new lines
        "/\t/" => '', // remove tab
        "/\s+/" => ' ', // remove spaces
    ];

    /**
     * @param  $htmlString  string
     * @return string
     */
    public function minify(string $htmlString): string
    {
        return preg_replace(array_keys($this->replace), array_values($this->replace), $htmlString);
    }
}