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.

Marin Sagovac
  • 3,932
  • 5
  • 23
  • 53

2 Answers2

0

This should work

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

Bye

PatomaS
  • 1,603
  • 18
  • 25
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)

Community
  • 1
  • 1
Francois Bourgeois
  • 3,650
  • 5
  • 30
  • 41