Minify html output from content sql table

470 Views Asked by At

On the admin page, im writing content with the CK editor, and i echo this content on my website.

<div class="content"><?php echo $content['text']; ?></div>

I attach an image, you can see this content output source code. enter image description here

How can i minify or compress this generated output? I want to get smaller page size, and faster load.

Is there any php function for this, that i can use easy?

I find some on the site, but they dont workd, or they needed php.ini config, and i dont want that.

1

There are 1 best solutions below

5
On BEST ANSWER

The best solution is turning on gzip compression on server. Removing spaces and line breaks via php won't give you better result, same as using both technologies will give result 99% - gzip and 1% - removed spaces.

But you can try something like this:

<div class="content"><?php echo preg_replace(
    array(
        '/ {2,}/', //remove multiply spaces 
        '/<!--.*?-->|\t|(?:\r?\n[ \t]*)+/s' //remove comments, tabs, empty lines
    ),
    array(
        ' ',
        ''
    ),
    $content['text']
);?></div>

If you need to remove html entities from content, and you call this "minify" use html_entity_decode($content['text']) this will decode &lq; and others encoded symbols to their normal state