0

I have generated PHP files for cache loading (via file_get_contents() or include() which randomly procedures...) and let's considere this example:

Un-optimized mode or uncached:

What I NOT want to see:

  • spaces between <body> and <head>
  • spaces between closed tags: /> <script or <table> <tr>

What I WANT to see (optimized way using PHP maybe PREG_REPLACE):

  • trimed spaces between tags <body><head>
  • trimed spaces between closed tags: /><script or <table><tr>

For above full example should be as RESULT such as:

<!DOCTYPE><head><link href="/static/css/main.css" rel="stylesheet" type="text/css"><title>Title</title><meta http-equiv='Content-Type' content='Type=text/html; charset=UTF-8'> <meta name="description" content="Description site" /><meta name="keywords" content="keywords, another keywords" /><script type="text/javascript" src="/static/js/jquerymin.js"></script></head><body><div id="center_box"><div id="orderdata"><table></table></div><div id="orderform"><form method="post" action="/frontacp" method="post" action="/frontacp" method="post" action="/frontacp" method="post" action="/frontacp"><div class="logincol"><div class="leftcol"><label for="types_name">Tip proizvoda</label></div><div class="rightcol"><input type="text" name="types_name" /></form></div></div></body></html>

Instead BAD (unoptimized usage) of page:

    <!DOCTYPE><head>
    <link href="/static/css/main.css" rel="stylesheet" type="text/css"><title>Title</title>
    <meta http-equiv='Content-Type' content='Type=text/html; charset=UTF-8'> <meta name="description" content="Description site" /><meta name="keywords" content="keywords, another keywords" /><script type="text/javascript" src="/static/js/jquerymin.js"></script></head>
    <body><div id="center_box">
        <div id="orderdata">
        <table>

        </table>
        </div>

        <div id="orderform">
        <form method="post" action="/frontacp" method="post" action="/frontacp" method="post" action="/frontacp" method="post" action="/frontacp"><div class="logincol"><div class="leftcol"><label for="types_name">Tip proizvoda</label></div><div class="rightcol"><input type="text" name="types_name"    /></form>    </div>
    </div></body></html>

Solutions: PHP example to trim this problem using in properly method and wan't trim spaces inner HTML tags.

Notice: I have done about reading cache and rendering this question would not cover in this area.

Thank you.

2 Answers 2

0

This should work

$document = preg_replace( '/\n/', '', $document );
$document = preg_replace( '/>\s+</', '><', $document );
$document = preg_replace( '/\s{2,}/', ' ', $document );

Bye

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

1 Comment

This tested and can be fixed.
0

This is a bit more complex than one might think and it raised a lot of questions here. I took the liberty to find one that seems to have a proper solution:

minifying-final-html-output-using-regular-expressions-with-codeigniter

(just ignore the reference of CodeIgniter there, since the solution is just a regex)

Comments

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.