NotionCommotion Posted February 26, 2016 Share Posted February 26, 2016 Had some JS that wasn't working, and turned out I had a whitespace at the end of a PHP file. To fix it and other bad files, came up with the following. See any problems with it? Yea, I know that it surely could be done at the command line, but made me feel safer doing it this way. Also, maybe should just get rid of the closing PHP tags altogether, but still have the issue with opening tags. Thanks <?php $dir=dirname(dirname(dirname(__FILE__))).'/classes'; echo($dir."\n\n"); $dir = new DirectoryIterator($dir); foreach ($dir as $fileinfo) { if (!$fileinfo->isDot() && $fileinfo->getExtension()=='php') { $file=$fileinfo->getPathname(); $content=file_get_contents($file); if(ctype_space(substr($content, 0, 1)) || ctype_space(substr($content, -1))){ echo("TRIM: $file\n"); file_put_contents($file, trim($content)); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/300891-remove-whitespace-from-php-file/ Share on other sites More sharing options...
maxxd Posted February 26, 2016 Share Posted February 26, 2016 Get rid of the final closing tags on all your php files. Any white space after the closing tag will be output to screen and can cause issues in many different ways. Quote Link to comment https://forums.phpfreaks.com/topic/300891-remove-whitespace-from-php-file/#findComment-1531498 Share on other sites More sharing options...
NotionCommotion Posted February 26, 2016 Author Share Posted February 26, 2016 Yea, I guess I should just get in the habit of doing so. Quote Link to comment https://forums.phpfreaks.com/topic/300891-remove-whitespace-from-php-file/#findComment-1531499 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.