当前位置:多学网学习教育电脑学习编程入门PHP教程PHP 源代码压缩小工具

PHP 源代码压缩小工具

[08-23 22:08:37]   来源:http://www.duoxue8.com  PHP教程   阅读:504
PHP 源代码压缩小工具,标签:PHP技巧,php培训,php学习,php安装,http://www.duoxue8.com

所有的PHP文件包在一个PHP文件
移除所有的注释
移除多余空格
保持输出纯PHP(即不需要任何额外的处理步骤来解析这个文件)

使用方法:(在命令行运行)

 
php compactor.php DESTINATION.php SOURCE.php
php 代码复制内容到剪贴板
  1. #!/usr/bin/env php   
  2. <?php   
  3. /**  
  4.  * Compact PHP code.  
  5.  *  
  6.  * Strip comments, combine entire library into one file.  
  7.  */  
  8.     
  9. if ($argc < 3) {   
  10.   print "Strip unecessary data from PHP source files.nntUsage: php compactor.php DESTINATION.php SOURCE.php";   
  11.   exit;   
  12. }   
  13.     
  14.     
  15. $source = $argv[2];   
  16. $target = $argv[1];   
  17. print "Compacting $source into $target.n";   
  18.     
  19. include $source;   
  20.     
  21. $files = get_included_files();   
  22. print_r($files);   
  23.     
  24. $out = fopen($target, 'w');   
  25. fwrite($out, '<?php' . PHP_EOL);   
  26. fwrite($out, '// QueryPath. Copyright (c) 2009, Matt Butcher.' . PHP_EOL);   
  27. fwrite($out, '// This software is released under the LGPL, v. 2.1 or an MIT-style license.' . PHP_EOL);   
  28. fwrite($out ,'// http://opensource.org/licenses/lgpl-2.1.php');   
  29. fwrite($out, '// http://querypath.org.' . PHP_EOL);   
  30. foreach ($files as $f) {   
  31.   if ($f !== __FILE__) {   
  32.     $contents = file_get_contents($f);   
  33.     foreach (token_get_all($contents) as $token) {   
  34.       if (is_string($token)) {   
  35.         fwrite($out, $token);   
  36.       }   
  37.       else {   
  38.         switch ($token[0]) {   
  39.           case T_REQUIRE:   
  40.           case T_REQUIRE_ONCE:   
  41.           case T_INCLUDE_ONCE:   
  42.           // We leave T_INCLUDE since it is rarely used to include   
  43.           // libraries and often used to include HTML/template files.   
  44.           case T_COMMENT:   
  45.           case T_DOC_COMMENT:   
  46.           case T_OPEN_TAG:   
  47.           case T_CLOSE_TAG:   
  48.             break;   
  49.           case T_WHITESPACE:   
  50.             fwrite($out, ' ');   
  51.             break;   
  52.           default:   
  53.             fwrite($out, $token[1]);   
  54.         }   
  55.            
  56.       }   
  57.     }   
  58.   }   
  59. }   
  60. fclose($out);   
  61. ?>   

本文相关附件下载: 下载统计:次数:16  人气:139
  下载地址 coolcode.zip 0.9(KB)

PHP 源代码压缩小工具 结束。
Tag:PHP教程PHP技巧,php培训,php学习,php安装电脑学习 - 编程入门 - PHP教程