• Home
  • About
  • Services
    • Technical SEO
    • Local SEO Services
  • Plugins & Scripts
  • Tutorials Hub
    • Technical SEO
    • WordPress
    • PHP
    • JavaScript
    • Python
    • Google Tag Manager
    • Shopify
    • Other Guides & Tutorials
    • General Discussion
    • View All
  • Contact
    • Hub
      New posts Search forums
    • What's new
      New posts New profile posts Latest activity
    • Members
      Current visitors New profile posts Search profile posts
    Log in Register
    What's new Search

    Search

    By:
    Advanced search…
    • New posts
    • Search forums
    Menu
    Log in

    Register

    Install the app
    • Home
      • Hub
      • PHP Scripts & Snippets
      You are using an out of date browser. It may not display this or other websites correctly.
      You should upgrade or use an alternative browser.

      Minify HTML with this PHP function

      • Thread starter Chris
      • Start date Sep 12, 2021
      C

      Chris

      Administrator
      Staff member
      • Sep 12, 2021
      • #1
      PHP remove whitespace - Use this handy PHP function to remove all whitespace in HTML pages, effectively minifying the HTML page. This will shave off a few kb's on large HTML pages rendered using PHP.

      The snippet features are:

      1. Strip whitespaces after tags, except space
      2. Strip whitespaces before tags, except space
      3. Shorten multiple whitespace sequences
      4. Remove HTML comments
      Include the snippet below to one of your global files or add it directly to the HTML pages ending in .php
      Code:
      <?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");
      ?>


      The snippet above will remove the whitespace on HTML pages. It uses a PHP function to strip out the whitespace between the HTML elements.
       
      You must log in or register to reply here.
      Share:
      Facebook Twitter Reddit Pinterest Tumblr WhatsApp Email Share Link

      New Posts

      • C
        Bulk Create WordPress Categories using the REST API & Python
        • Started by Chris
        • Nov 3, 2022
        • Replies: 0
        Python Scripts & Tutorials
      • C
        Xenforo API - Bulk Post Threads (Python)
        • Started by Chris
        • Oct 7, 2022
        • Replies: 0
        Python Scripts & Tutorials
      • C
        Xenforo API - Python: Create new Thread
        • Started by Chris
        • Oct 6, 2022
        • Replies: 0
        Python Scripts & Tutorials
      • C
        Xenforo API - Python Reply to Thread
        • Started by Chris
        • Oct 6, 2022
        • Replies: 0
        Python Scripts & Tutorials
      • C
        Bulk Remove Meta Robots Tag using Google Tag Manager
        • Started by Chris
        • Sep 26, 2022
        • Replies: 1
        Google Tag Manager Tutorials & Guides
      • C
        Python Script to Bulk Upload new Wordpress Posts from a CSV file using WordPress Rest API
        • Started by Chris
        • Sep 5, 2022
        • Replies: 0
        Python Scripts & Tutorials
      • C
        Python & WordPress Rest API - Auto Publish a Post
        • Started by Chris
        • Sep 3, 2022
        • Replies: 0
        Python Scripts & Tutorials
      • C
        Screaming Frog v17 - Issues Summary Report & Export
        • Started by Chris
        • Aug 17, 2022
        • Replies: 0
        Technical SEO
      • Hub
      • PHP Scripts & Snippets

    Technical SEO Consultant

    Chris Lever - Freelance SEO Consultant Availible for Hire. Specialist in Technical SEO Audits, Strategy and Implementation.

    Need Help?

    • BB Code
    • Cookie Policy
    • Terms of Use
    • Privacy Policy
    • HTML Sitemap

    Learn More

    • About
    • Availability
    • Popular Posts
    • Projects & Scripts
    • Contact

    Get in Touch

    Chris Lever, Manchester, United Kingdom
    info@chrisleverseo.com
    +44 0161 883 2249

    © 2023 Chris Lever SEO. All rights reserved. Powered by Xenforo

    • This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
      By continuing to use this site, you are consenting to our use of cookies.
      Accept Learn more…