php 怎样批量修改文本内容?-PHP问题

资源魔 24 0

php 怎么批量修正文本内容?

php 批量修正文本内容的办法:

//列出目次下文件
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 '++++++++++'. $file.'++++++++++<br />';
                    }
                    else echo '------非PHP文件------<br />';
                    $num++;
                }
            }
        }
        closedir($handle);
    }
}
function myScanDir($path) {
    $result = scandir($path);
    foreach($result as $re) {
        if($re == '.' || $re == '..') continue;
        $dir = $path.'/'.$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);
}

操作文件的函数有不少有用的:

file()把文件读入一个数组中,(即可以逐行显示文件内容)

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

没有把文件外在读进去放到另人变量里的,间接读取文件缓存中的内容

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

保举教程:《PHP视频教程》

以上就是php 怎么批量修正文本内容?的具体内容,更多请存眷资源魔其它相干文章!

标签: php教程 php故障解决 php使用问题 修改文本

抱歉,评论功能暂时关闭!