Simple Whitespace Reduction In HTML

<?php /** * Simple Whitespace Reduction In HTML Output * * This function will simply reduce all * whitespace found in your HTML. Additionally, * it will also inline your HTML output--all on * one single line--for optimal load times. * * * THIS IS YOUR HTML BEFORE * * <html> * * <head></head> * * <body> * * <div> * * <p>This is already a long HTML output.</p> * * </div> * * </body> * * </html> * * * THIS IS YOUR HTML AFTER * * <html> <head></head> <body> <div> <p>This is already a long HTML output.</p> </div> </body> </html> * * For more details, please view your website's `(index)` inside the ChromeDev Sources tab. * * @author Davey Jacobson {@link https://twitter.com/DaveyJacobson} * @params $buffer string Name of function for output buffering. `ob_start("remove_whitespace")` * @return mixed */ function remove_whitespace($buffer){ return preg_replace("/(\s+)/s", " ", $buffer); }
All this function does is look for consecutive white-spaces (e.g. tabs, new line breaks, or anything that is greater than or equal 2+ presses of the spacebar) and reduces it to one single space. Example in source code.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.