批量替换文本内容-PHP操作文件方法
2012-03-01 17:15
621 查看
//列出目录下文件
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);
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);
相关文章推荐
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- PHP使用文件流下载文件方法(附:解决下载文件内容乱码问题)
- php读取文本文件内容
- php实现批量删除挂马文件及批量替换页面内容完整实例
- JavaScript使用FileSystemObject对象写入文本文件内容的方法
- Python文件操作,open读写文件,追加文本内容
- php生成txt文件标题及内容的方法
- Java:InputStream读取文本文件内容出现乱码问题的解决方法
- PHP文件操作方法汇总
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- thinkphp相对路径问题及其使用php原生文件操作的方法
- php编程实现追加内容到txt文件中的方法
- php读取torrent种子文件内容的方法(测试可用)
- PHP操作文件方法问答
- 【Php】遍历目录下文件、批量替换文件内容等功能的一个类
- php 生成xls文件utf8转gbk丢失内容的解决方法
- PHP实现对文件锁进行加锁、解锁操作的方法
- C#实现写入文本文件内容的方法
- php读取torrent种子文件内容的方法(测试可用)
- PHP数组的初始化 访问数组 数组操作符 多维数组的排序 对数组重新排序 从文件载入数组的方法 数组其他有用的操作 统计数组个数 将数组转换称标量变量 extract()