×
  • Shared Hosting

    Fast reliable and affordable cPanel Web Hosting from $2.99 per month.

  • Reseller Hosting

    Multiple websites? No problem with our multi-site hosting package. From $5.99 per month.

  • Annual Hosting

    Save money on web hosting by paying annually. Starting at $29.99 per year.

  • VPS Servers

    Need more power and resources? Choose our VPS server, only $7.99 per month.

  • FREE HOSTING PLAN

    Ideal for students and unemployed.

    Register

PHP script to remove the white space and comments

17/03/2022 | |0 comment

As the importance of website performance continues to rise, developers are taking a closer look at their code to ensure that the sites they create are running as quickly and efficiently as possible. One of the most effective ways to improve the performance of a website is to remove whitespace and comments from the code.

Whitespace is any sequence of characters that is used to separate words or lines of text. It can also refer to blank lines or tabs in the HTML, CSS, or JavaScript code of a website. While whitespace does not necessarily affect the functionality of a website, it does take up extra space, which can cause the website to appear slower and less responsive. Removing whitespace from a website’s code can help to reduce the overall size and improve the speed of the site.

Additionally, comments are also often included in website code. Comments are used by developers to explain the code and make it easier to read. However, comments take up extra space and can slow down a website. Removing comments from a website‘s code can help to reduce the amount of data that needs to be transmitted and can help improve the site‘s speed.

Below is a simple-to-use PHP script to remove the white space and comments

<?php 

/* remove whitespace */ 
$commwhite = preg_replace('/\s+/', '', $string); 

/* remove comments */ 
$commwhite = preg_replace('#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#', '', $string); 

echo $commwhite; 
?>

 

Chris

Freelance Technical SEO with - https://sitebee.co.uk