PHP代码加密 -- php_strip_whitespace函数,去掉源代码所有注释和空格并显示在一行

<?php

function stripCommentAndWhitespace($path = '')
{
    if (empty($path) || !is_dir($path)) {
        exit('请指定要操作的文件路径');
    }

    @chmod($path, 0755);
    $path = str_replace('\\', '/', $path);

    if ($handle = opendir($path)) {
        while (false !== ($fileName = readdir($handle))) {
            if ($fileName != '.' && $fileName != '..') {
                if (is_file($path . '/' . $fileName)) {
                    // 压缩.php后缀文件
                    $suffix = pathinfo($path . '/' . $fileName, PATHINFO_EXTENSION);
                    if ($suffix == 'php') {
                        $newFile = php_strip_whitespace($path . '/'. $fileName);
                        file_put_contents($path . '/'. $fileName, $newFile);
                    }
                    continue;
                }

                if (is_dir($path . '/' . $fileName)) {
                    stripCommentAndWhitespace($path . '/' . $fileName);
                }
            }
        }
        closedir ( $handle );
    }

    exit('<center>注释与空白清理完毕</center>');
}

// 调用
stripCommentAndWhitespace('F:\Apache\htdocs\site_test');

?>