How to minify php page html output?
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:
<?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");
?>
How to minify php html output without removing non empty newlines?
$data = '
<!DOCTYPE html>
<html>
<head>
<title>Untitled Document</title>
<meta name="description" content="Phasellus rhoncus euismod libero a lacinia. Lorem ipsum dolor sit amet, consectetur adipiscing elit." />
</head>
<body>
<div id="content">
<div id="post-1">
<h1>Phasellus rhoncus euismod libero a lacinia.</h1>
<p>...</p>
</div>
<div id="post-2">
<h1>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</h1>
<p>...</p>
</div>
</div>
</body>
</html>';
echo "<pre>";
echo htmlentities(preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $data));
echo "</pre>";
Minify html output from content sql table
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
Minify HTML, but don't touch PHP with Gulp
The gulp-htmlmin module uses the html-minifier module, which has plenty of options (displayed on both its npmjs.com and github pages) that can be used. The option we will focus on is ignoreCustomFragments
.
var gulp = require(gulp),
htmlmin = require(gulp-htmlmin);
gulp.task('htmltask', function(){
return gulp.src(['./dev/*.html','./dev/*.php'])
.pipe(htmlmin({
collapseWhitespace: true,
ignoreCustomFragments: [ /<%[\s\S]*?%>/, /<\?[=|php]?[\s\S]*?\?>/ ]
}))
.pipe(gulp.dest('./site'));
});
In the above code, you see we are using ignoreCustomFragments
with the regex /<\?[=|php]?[\s\S]*?\?>/
to ignore code starting with <?
and <?php
and ending with ?>
.
By default, html-minifier ignores php, so you don't have to worry about setting ignoreCustomFragments
.
EDIT
Thanks amersk
Some php files you work with may not have closing tags, for example many WordPress files do not. An alternative would be to use the following instead:
ignoreCustomFragments: [/<\?[\s\S]*?(?:\?>|$)/]
How to compress html output in php?
That symbol means it's a foreign character and your particular font doesn't know what character it needs to use. You should look into multibyte-safe string functions and UTF-8 encoding and decoding
Related Topics
PHP: Convert Any String to Utf-8 Without Knowing the Original Character Set, or At Least Try
Performance of For VS Foreach in PHP
Getting the Names of All Files in a Directory With PHP
How to Increase Maximum Execution Time in PHP
How to Benchmark Efficiency of PHP Script
MySQLi: Can It Prepare Multiple Queries in One Statement
How to Retract a Salted Password from the Database and Auth User
How to Find Out If You'Re Using Https Without $_Server['Https']
How to Use Curl to Get Json Data and Decode the Data
Running MySQL *.SQL Files in PHP
Speed Difference in Using Inline Strings VS Concatenation in PHP5
What Does PHP Keyword 'Var' Do
How to Extend Access Token Validity Since Offline_Access Deprecation