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

时间:2024-11-29 08:35:49

<?php

function stripCommentAndWhitespace($path = '')
{
  if (empty($path)) {

    echo '请指定要操作的文件路径';
    return false;

  } else {

    $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);

          }

        }

        if (is_dir($path . '/' . $fileName)) {

          stripCommentAndWhitespace($path . '/' . $fileName);

        }

      }

    }

    closedir ( $handle );

  }

}

stripCommentAndWhitespace('YourProjectNamePath');

?>