从php源代码中删除空格

A friend of mine is trying to remove whitespace and comments from a whole application. I think his intent is that no one can really mess with the code. He, himself, will jave the 'raw' copy.

Is there a way to remove whitespace and comments from a whole application with folders and *.php and *.html files.

I've read this question but it says about minifying for a file: php source code whitespace

This should point you in the right direction:

http://php.net/manual/en/function.php-strip-whitespace.php

php -w file_source.php > file_result.php

should work with html too.

This is an old question, but the answers here weren't exactly what I needed but pointed me in the right direction. We wanted to minify the PHP code primarily to make it more difficult for people who really don't know what they are doing to mess with the code. It's not foolproof by any means, and won't stop people who know what they are doing from ripping off your code. Anyway we wanted to be able to run it as an option from our build process which is bash and perl based. This command will find all the php files and strip the whitespace and comments using php -w:

find . -name '*.php' -type f -exec sh -c 'php -w "${0%.*}.php" > "${0%.*}.cphp";
 rm "${0%.*}.php"; mv "${0%.*}.cphp" "${0%.*}.php"' {} \;

line break added for clarity, this should be entered on one line.

I'm not a shell expert so this may need some cleanup, but we use it and it works. We only use it after the code has been staged on the server, never on the actual development source.

Also note that since it's find, all kinds of filtering options are available, as well as setting -maxdepth to restrict it from acting on sub-folders.

Essentially it's just using the find command and executing a shell that minifies the php to a new file, removes the original file and renames the new file to the original name.

Again this should only be used once the code has been deployed to a test server or to production, never on your active development code.

We also do something similar for css and js files...