![]() |
CSS and JavascriptConsider the following link to minify Javascript/CSS files: https://github.com/mrclay/minify HTMLTell 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.)
Use the following snippet to remove white-spaces from the HTML with the help ob_start's buffer: |
Put following code very top of your site.
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");
?>
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");?>
This work for me on php apache wordpress + w total cache + amp put it on the top of php page
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);
}
?>
html minify |
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 |
How to minify all html code of Your Website | PHP Cache Tutorial |
How to minify html code of Your Website | PHP Cache Tutorial |
How to make short all codes of your website | PHP 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