Home  >  Article  >  php教程  >  PHP recursively implements formatting of all json files

PHP recursively implements formatting of all json files

WBOY
WBOYOriginal
2016-12-05 13:27:461608browse

Divided into two, 1: Do not consider spaces in json data
2: Consider that the json file has spaces
<?php<br> /*<br> *time: December 3, 2016 16:42:46<br> *author:zhangchennyang<br> *Function: json file traversal compression<br> *<br> */<br> <br> header('content-type:text/html;charset=utf8');<br> fRename('../json'); //Use this method parameter to set the folder directory<br> function fRename($dirname)<br> {<br> If (!is_dir($dirname)) {<br> echo "{$dirname} is not a valid directory!";<br> exit();<br> }<br> $handle = opendir($dirname);<br> While (($fn = readdir($handle)) !== false) {<br> If ($fn != '.' && $fn != '..') {<br> <br>                                                                                                                                                                                                                                                                                                          . $curDir = $dirname . '/' . $fn;<br> <br>                       // echo "curDir = " . $curDir . "rn";<br>                  if (is_dir($curDir)) {<br>                                        fRename($curDir);<br>                     } else {<br> <br>                   $path = pathinfo($curDir);<br>                         //var_dump($curDir);<br>                   $file_type=$path['extension'];<br> If($file_type=='json'){<br> $ Str = File_get_Contents ($ Curdir); // Read the text based on the folder to obtain the string <br>                                                                                                                                                                                                                                                                                                          through                                                                                          <br> $ K_str = json_encode ($ arr); // The array convert it to a string <br>                             $statu=file_put_contents($curDir,$k_str);<br>                                                                           If($statu){ //Formatting successful<br>                       $time=date('y-m-d h:i:s'); //Execution time<br>                        $wenjian=$curDir;<br> $ok_= 'file' . $wenjian . '---time:' . $time.'-'.PHP_EOL;<br> file_put_contents('ok.txt',$ok_, FILE_APPEND);<br>                                                                                                                                                                                                                                                                                                                                             }else{<br>                         $time=date('y-m-d h:i:s'); //Execution time<br>                           $wenjian=$curDir;<br>                       $error_info= 'file' . $wenjian . '---time:' . $time.'-'.PHP_EOL; <br> file_put_contents('error.txt',$error_info, FILE_APPEND);<br>                     }<br>                                                                                                                                                                     }<br> <br>          }<br> <br> }<br> }<br> <br> <br> <br> //Remove spaces and other strings<br> /*function trimall($str){<br> $qian=array(" "," ","t","n","r");<br> $hou=array("","","","","");<br> Return str_replace($qian,$hou,$str); <br> <br> }*/<br> <br>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn