Getting more and more into javascript coding, two tools I found I could not do without are JSMin and JSlint, from Douglas Crockford.
On the JSmin webpage, a php version is available for download, but it did not fit my needs really well (in fact, the js version did, but, being a php-head, I got that one first), so I patched it a bit:
- made all the code work with PHP 4 (removed usage of splfileiterator, exceptions are replaced by triggering errors, class constants are turned into global constants, etc…)
- constant VERSION changed to JSMIN_VERSION to avoid name clashes
- added a new function to class JSMin to cleanly separate writing to output stream from parsing
- make sure that newline chars passed inside the comments parameter do not break output
- the file can now be included in a php app (whether cli or web hosted) and used as library (just define JSMIN_AS_LIB before including jsmin.php)
- when used as library, the class can operate on php strings instead of files
- i could not resist the urge to remove excess whitespace here and there in the php source, too (phpmin-syndrome???)
The new code is available here. Take a look at the comments inside for more info.
There is some other (php) work done on jsmin from a guy named Ed Eliot there
This is what I searched! 🙂 I combined your class with the php port of Dean Edward’s packer. It works like a charm! Fantastic! Thank you! :))
Without sounding stupid… how would i use this class assuming that the entire js content is a string contained within $this_string.
As you can easily read in the comments in the code 😉
minify();
?>
Hi,
Thanks for this work ! I made a version that use more PHP native functions to speed up the process (though the gain is not huge). I get rid of code dealing with files too, because it’s easier to use PHP functions (like file_get_contents) and minify it as a string.
Test page is here : http://www.the-asw.com/public/web/myjsmin/
More explainations here (in french) : http://www.the-asw.com/post/2007/03/28/Une-version-optimisee-pour-PHP-de-JSMin
Regards