Horje
How to minify all code of Your Website

CSS and Javascript

Consider the following link to minify Javascript/CSS files: https://github.com/mrclay/minify

HTML

Tell Apache to deliver HTML with GZip - this generally reduces the response size by about 70%. (If you use Apache, the module configuring gzip depends on your version: Apache 1.3 uses mod_gzip while Apache 2.x uses mod_deflate.)

Accept-Encoding: gzip, deflate

Content-Encoding: gzip

Use the following snippet to remove white-spaces from the HTML with the help ob_start's buffer:


How to minify all html code of Your Website

Put following code very top of your site.

index.php
Example: PHP
<?php

function sanitize_output($buffer) {

    $search = array(
        '/\>[^\S ]+/s',     // strip whitespaces after tags, except space
        '/[^\S ]+\</s',     // strip whitespaces before tags, except space
        '/(\s)+/s',         // shorten multiple whitespace sequences
        '/<!--(.|\s)*?-->/' // Remove HTML comments
    );

    $replace = array(
        '>',
        '<',
        '\\1',
        ''
    );

    $buffer = preg_replace($search, $replace, $buffer);

    return $buffer;
}

ob_start("sanitize_output");

?>

Output should be:

How to minify all html code of Your Website

How to minify html code of Your Website

Keep following code very top of your side page.

Example: PHP
<?php
function minify_output($buffer){
    $search = array('/\>[^\S ]+/s','/[^\S ]+\</s','/(\s)+/s');
    $replace = array('>','<','\\1');
    if (preg_match("/\<html/i",$buffer) == 1 && preg_match("/\<\/html\>/i",$buffer) == 1) {
        $buffer = preg_replace($search, $replace, $buffer);
    }
    return $buffer;
}
ob_start("minify_output");?>

Output should be:

How to minify html code of Your Website

How to make short all codes of your website

This work for me on php apache wordpress + w total cache + amp put it on the top of php page

index.php
Example: PHP
<?Php
if(substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')){
    ob_start ('ob_html_compress'); 
}else{
    ob_start(); 
}
function ob_html_compress($buf) {
$search = array('/\>[^\S ]+/s', '/[^\S ]+\</s', '/(\s)+/s', '/<!--(.|\s)*?-->/');
$replace = array('>', '<', '\\1', '');
$buf = preg_replace($search, $replace, $buf);
return $buf;

return str_replace(array("\n","\r","\t"),'',$buf);
}
?>

Output should be:

How to make short all codes of your website




html minify

Related Articles
How to create PHP Simple HTML Cache for a Web Page PHP Cache Tutorial
How to minify all code of Your Website PHP Cache Tutorial

Single Articles
How to minify all html code of Your WebsitePHP Cache Tutorial
How to minify html code of Your WebsitePHP Cache Tutorial
How to make short all codes of your websitePHP Cache Tutorial

Read Full:
PHP Cache Tutorial
Type:
Develop
Category:
Web Tutorial
Sub Category:
PHP Cache Tutorial
Uploaded:
5 months ago
Uploaded by:
Admin
Views:
213


Reffered: https://stackoverflow.com/questions/6225351/how-to-minify-php-page-html-output

Share on: