Home  >  Article  >  Backend Development  >  How to batch modify text content in php?

How to batch modify text content in php?

coldplay.xixi
coldplay.xixiOriginal
2020-06-11 10:50:012677browse

How to batch modify text content in php?

#php How to modify text content in batches?

php Method to modify text content in batches:

//列出目录下文件
function file_list($path){
    $num = 0;
    if($handle = opendir($path)){
        while(false !== $file=readdir($handle)){
            if($file !='.' && $file !='..'){
                if(is_dir($path. '/'. $file)){
                    file_list($path. '/'. $file);
                }else{
                    if(preg_match ("/.php$/", $file)){  //这里匹配是否PHP文件
                        file_content_replace($path.'/'.$file, 'PPCART_TEXT_SUCCESS', '***********'); //这里的替换方式;开始替换文本
                        echo &#39;++++++++++&#39;. $file.&#39;++++++++++<br />&#39;;
                    }
                    else echo &#39;------非PHP文件------<br />&#39;;
                    $num++;
                }
            }
        }
        closedir($handle);
    }
}
function myScanDir($path) {
    $result = scandir($path);
    foreach($result as $re) {
        if($re == &#39;.&#39; || $re == &#39;..&#39;) continue;
        $dir = $path.&#39;/&#39;.$re;
        if(is_dir($dir)) {
            showDir($dir);
            continue;
        }
        echo $dir."<br />";
    }
}
//比较替换参数依:文件名、被替换字符串、替换字符串;file_get_contents(),file_put_contents(),str_replace()配合使用
function file_content_replace($filename, $search, $replace){
    $string = file_get_contents($filename);
    $new_string = str_replace($search, $replace, $string);
    if($string !=$new_string) file_put_contents($filename, $new_string);
}

There are many useful functions for operating files:

file( ) Read the file into an array, (you can display the file content line by line)

//逐行显示文件内容
function show_line($file){
    $lines = file($file);
    foreach($lines as $line_num => $line){
        if(preg_match ("/PPCART_TEXT_SUCCESS/", $line)){
            $add = &#39;********************math********************&#39;;
        }else{
            $add = &#39;-----------------------------------------------&#39;;
        }
        echo "Line #<b>{$line_num}</b>: ". htmlspecialchars($line). $add. &#39;<br />&#39;;
    }
    $line = NUll;
    $lines = NUll;
    $line_num = NUll;
}

Do not read out the file content and put it into another variable, directly read the content in the file cache

$fp = fopen($file, &#39;r&#39;);  //以读方式打开文件;$fp = fopen($file, &#39;r+&#39;);//以读写方式打开;$fp = fopen($file, &#39;a&#39;);//以写追加方式打开
// read some data
$data = fread($fp, 18);  
  //fgetc()读字符串 | fgets()读行 | fgetss()读行并去标签 | fread()读文件 | stream_get_line()读文件
var_dump($data);echo &#39;-----------<br />&#39;;
// move back to the begining of the file
// same as rewind($fp);
fseek($fp, 10);  //设置文件指针
$data = fread($fp, 9);
var_dump($data);echo &#39;<br />&#39;;
fclose($fp);

Recommended tutorial: "PHP Video Tutorial"

The above is the detailed content of How to batch modify text content in php?. For more information, please follow other related articles on the PHP Chinese website!

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