search
Homephp教程php手册PHP recursively implements formatting of all json files

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function