Created
August 13, 2025 18:55
-
-
Save atelierbram/c51677fb350309fe83e016bb1730d52a to your computer and use it in GitHub Desktop.
Compress output PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Start output buffering and clean whitespace | |
ob_start(function ($buffer) { | |
// Remove tabs, newlines, multiple spaces between tags | |
$buffer = preg_replace('/\s+/', ' ', $buffer); | |
// Remove spaces between HTML tags | |
$buffer = preg_replace('/>\s+</', '><', $buffer); | |
return trim($buffer); | |
}); | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Minimised Output</title> | |
</head> | |
<body> | |
<section> | |
<h1> My Title </h1> | |
<p> | |
This paragraph | |
has extra spaces and newlines. | |
</p> | |
</section> | |
</body> | |
</html> | |
<?php | |
// Send the minified HTML | |
ob_end_flush(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment