0

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 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, it works. Can i use this anywhere where i echo out a html content? It wont mess up my content?
regexp can damage some content if spaces or empty lines in is necessary
For example? Please so me something.
for example you have something that needs <pre> wrapper. Text with empty lines placed by author to separate article sections
In the content, i never use <pre> tags, only text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.