Example method for PHP recursively traversing folders to remove comments and compress php source code

  • 2021-10-11 17:58:55
  • OfStack

This article illustrates how PHP recursively traverses folders to remove comments and compress code. Share it for your reference, as follows:


<?php
// =================== Folder   Recursion   Removal PHP Annotate and compress =========================
header("content-type:text/html;charset=utf-8");
$dir = "D:\sound\admin--";  //  The file path to be converted. 
$fdir = "D:\sound\admin";  //  The final position to be put. 
$dir = str_replace('\\','/',$dir);
$fdir = str_replace('\\','/',$fdir);
if($dir == $fdir){
 echo " The source file conflicts with the current file path, change 1 Let's take a path ";
 exit;
}
$arr = array($dir,$fdir);
ergodic($dir,$arr); //  Directory name when recursive 1 Straight is changing. So use 1 An array of two additional paths is stored so that files or directories can be found in that directory when they are created. 
md($fdir);
function md($md){ //  Create Directory 
 if(!is_dir($md) && !empty($md) ){
    mkdir($md,0777);
 }
}
//  Recursion 
function ergodic($dirname,$arr){
  if(is_dir($dirname)){
    $handle = opendir($dirname);
    $a = $arr[1].substr($dirname,strlen($arr[0]) );
    md($a);
    while($filename = readdir($handle)){  //  Read 1 The name of an open file. 
      if($filename != '.' && $filename != '..'){
        $dir = $dirname.'/'.$filename; //  Splice directory name 
        if(is_dir($dir)){
          ergodic($dir,$arr);  //  Recursion 
        }else{
          file_put_contents($a.'/'.$filename,replace_php_src($dir) );
          echo $dir."   To comment compression complete! <br>";
        }
      }
    }
  }else{
    //  This is useless (because all the directories come). If you only convert 1 File, you can use it like this. 
   file_put_contents($arr[1].'/'.$dirname,replace_php_src($arr[0].'/'.$dirname) );
  }
}
//  Remove comments and compress, heredoc When there is a comment, it is not compressed. 
function replace_php_src($src){
  $contents = file_get_contents($src);
  $num = substr_count($contents,'<<<');  // heredoc  Whether it exists. 
  $str = "";
  if($num > 0){  // heredoc  Existence. Remove comments only without compression. 
      $file = token_get_all($contents); // token_get_all()  Press the supplied source code  PHP  Mark for segmentation .
      for ($i=0; $i < count($file); $i++) {
        if( is_string($file[$i]) ){
          $str .= $file[$i];
        }else{
           $name = token_name( $file[$i][0] ); // token_name()  Gets the provided  PHP  Symbolic name of parser code .
            if($name == 'T_COMMENT' || $name == 'T_DOC_COMMENT' ){ //  Remove comments 
                continue;
            }else{
              $str .= $file[$i][1];
            }
        }
      }
  }else{
    $str = php_strip_whitespace($src); //  Nonexistent  heredoc  . Because he will misinterpret. 
  }
  return $str;
}

More readers interested in PHP can check the topics of this site: "Summary of PHP Directory Operation Skills", "Summary of php File Operation", "Summary of PHP Common Traversal Algorithms and Skills", "PHP Data Structure and Algorithm Tutorial", "Summary of php Programming Algorithms", "Complete Collection of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: