Strip out comments, whitespaces & new lines in PHP files

I was working on a little script that creates an MD5 check sum for phpFoX Konsort, which will help the support technicians see if a client has modified a file or if they are using the default build released. Earlier I had used the most common method in doing this which is:

$sHash = md5(file_get_contents('filename.php'));

Another alternative to that is:

$sHash = md5_file('filename.php');

Both worked for the most part, but I found some abnormalities on windows servers and in numerous cases where files would get corrupted during an upload of a file via an FTP client. The corruption of the file would be that it would have extra whitespace’s and extra new lines added to the source code, which would still allow the script to function per its design but will also appear to be modified since any changes to the file would cause a drastic change in the MD5 hash.

One workaround I have found and thus far has been rather successful is to first strip out any comments, whitespace and new lines in the PHP source code. This would in return hash only the actual PHP code needed for the software to function.

If you use PHP5 you could use the function php_strip_whitespace, however this function has trouble when it comes to HTML embedded in the PHP source code. It does not remove any of the new lines, which would also cause drastic changes in an MD5 hash. Not to mention it only works on PHP5 and if you had to run the hash check on a PHP4 server it would return a fatal error.

The function I speak off can be found here.

Usage:

print StripWhitespace('filename.php');

The method I used was to create an MD5 hash which could be done like so:

$sHash = md5(StripWhitespace($sFileName));

If you have any other methods in getting this sort of job done feel free to share.