This extension adds Douglas Crockford's jsmin functionality to PHP.
http://www.crockford.com/javascript/jsmin.html
This extension currently works with all versions of PHP 5.3 and PHP 5.4. Support for older verisons of PHP is not provided.
To install, download the source and build as follows:
cd /path/to/source
phpize
./configure
make install clean
Then, move the built module to your extensions directory, and enable the extension in php.ini by adding:
extension="jsmin.so"
Using jsmin is simple.
Use this function to minify JavaScript. It accepts a single string argument.
<?php
$javascript = <<<JS
/**
* My JavaScript Library
*/
var notes = "jsmin is easy!";
JS;
echo jsmin($javascript);
Example output is the following:
var notes="jsmin is easy!";
Returns error code of last call to jsmin().
Returns an error message (string) for the last call to jsmin().
- JSMIN_ERROR_NONE - No errors.
- JSMIN_ERROR_UNTERMINATED_STRING - Unterminated string.
- JSMIN_ERROR_UNTERMINATED_COMMENT - Unterminated comment.
- JSMIN_ERROR_UNTERMINATED_REGEX - Unterminated regex.
Discovery Communications developed a similar extension in-house for minifying bundled JavaScript.
I decided to take the most recent source from Douglas Crockford's JSMin and port / manage the extension for PECL.