温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php批量修改文本内容的方法

发布时间:2020-06-11 14:39:28 来源:亿速云 阅读:184 作者:元一 栏目:编程语言

PHP概述

PHP即“超文本预处理器”,是一种通用开源脚本语言。PHP是在服务器端执行的脚本语言,与C语言类似,是常用的网站编程语言。根据动态网站要求,PHP语言作为一种语言程序,其专用性逐渐在应用过程中显现,其技术水平的优劣与否将直接影响网站的运行效率。其特点是具有公开的源代码, 在程序设计上与通用型语言,如C语言相似性较高,因此在操作过程中简单易懂,可操作性强。

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 怎样批量修改文本内容?的详细内容,更多请关注亿速云其它相关文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI