liamoco Posted October 3, 2010 Share Posted October 3, 2010 Hi, I want to remove all extra spaces and line breaks that I have in my PHP, HTML, CSS and JavaScript files, to reduce the file size, I do not want to go through 100+ files and do this manually. Is there a PHP script that would do this? Any suggestions where I should start looking? Thanks Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted October 3, 2010 Share Posted October 3, 2010 Hello there Liamoco, try the following. <?php // Set Filename could use a "$_GET" variable here $fileName = 'file.php'; //Get the contents of the file $fileContents = file_get_contents($fileName); // Remove any comments from the contents of the file $reformatFile = preg_replace('/#.*/','',preg_replace('#//.*#','',preg_replace('#/\*(?:[^*]*(?:\*(?!/))*)*\*/#','',($fileContents)))); // A little wizardry to remove whitespace but always keep at least 1 $reformatFile = preg_replace('/\s+/', ' ', $reformatFile); // Create a new file with the code in it $newFile = fopen($fileName, 'w'); fwrite($newFile, $reformatFile); fclose($newFile) // Create a file with old code just incase it breaks $oldFile = fopen('_'.$fileName, 'w'); fwrite($oldFile, $fileContents); fclose($oldFile); ?> Appropriate commenting is within the file Tell me how it goes bud. Regards, Paul. Quote Link to comment Share on other sites More sharing options...
liamoco Posted October 3, 2010 Author Share Posted October 3, 2010 Works great thanks Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted October 3, 2010 Share Posted October 3, 2010 No bother bud Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.