php修改文件内容|php修改php文件内容

php修改文件内容|php修改php文件内容的第1张示图

1. 用PHP,怎么修改txt文本内的内容

<?phpheader("Content-type:text/html;charset=gbk");if(isset($_POST['submit'])){$editContent=$_POST['editContent'];//获取输入框的内容$res=file_put_contents("test.txt",$editContent);//执行修改if($res){echo'内容修改成功!';}else{echo'内容修改失败!';}}else{echo'请做出修改….';}?><formmethod="post"action=""><textareaname="editContent"cols="100"rows="15"><?phpechofile_get_contents("test.txt")?></textarea><buttonname="submit">确认</button></form>

2. php修改php文件内容

说实话看了你的写法,真的是相当的奇怪。你既然包含了yyid.php文件,后面又修改yyid.php文件的内容,这回不是相当于一个答人把自己给提起来吗?要不你就在修改完成后在包含,应该改为:

<?php$id=$_POST['pd'];if($id!=''){echo$id."我是中国人";$origin_str=file_get_contents('yyid.php');$update_str=str_replace($ping,$id,$orgin_str);file_put_contents('yyid.php',$update_str);}include'yyid.php';?>

3. PHP 修改文本文件内容

<?php//从文件中读取$path="1.txt";$fp=file($path);$arr=array();foreach($fpas&$line){$data=explode("=",$line);if(count($data)>1){$arr[]=array($data[0]=>$data[1]);}else{$arr[]=$line;}}//假设要修改ProctType为10setValue("ProctType","10",$arr);//var_mp($arr);//重新保存到文件$fp=fopen("2.txt","w");foreach($arras$row){if(is_array($row)){foreach($rowas$key=>$r){fwrite($fp,$key."=".$r);}}else{fwrite($fp,$row);}}fclose($fp);functionsetValue($name,$value,&$arr){foreach($arras$key=>$row){if(is_array($row)&&isset($row[$name])){$arr[$key][$name]=$value;//修改后记得加上换行$arr[$key][$name]=$arr[$key][$name]."";}}}?>

我测试了可以使用,如果可以请将两个问题都采纳下,谢谢。

4. php实现将一个文件中的内容替换

//用正则表达式去匹配//成功返回 true, $file 文件, $host 主机名,$ip 要替换的内新容IPedit('1.txt', 'aaa', '127.0.0.1');function edit($file, $host, $ip = NULL){ $content = @file_get_contents($file); if(!$content){ return false; } $host = preg_quote($host); $content = preg_replace('/(hostname[\s]+'.$host.'[\s]{0,}[\r\n]+hostip[\s]+)[^\r\n]+/', "$1 $ip", $content); return file_put_contents($file, $content) ? true : false;}

5. 用PHP实现 读取和修改文本文件内容的代码

/** * 读文件**/function read_file($filename){ $fp = fopen($filename, "r") or die("couldn't open $filename"); $read = fread($fp, filesize($filename)); fclose($fp); return $read;}/** * 写文件**/function write_file($filename, $buffer){ $fp = fopen($filename, "w") or die("couldn't open $filename"); flock( $fp, LOCK_EX ); $write = fputs($fp, $buffer); flock( $fp, LOCK_UN ); fclose($fp); return true;}/** * 修改(只是追加内容)**/function append_to_file($filename, $buffer){ $fp = fopen($filename, "a") or die("couldn't open $filename"); flock( $fp, LOCK_EX ); fputs($fp, $buffer); flock( $fp, LOCK_UN ); fclose($fp); return true;}/** * 测试**/$str = read_file('test.txt');echo $str;write_file('test2.txt', $str);append_to_file('test2.txt', "ABCD");其实,读文件有更简便的方法,你可以看看 file 和 file_get_contents 函数。 写文件也有现成的 file_ put_ contents 函数。

6. php如何修改文件里的内容(指定修改)

可以把文件内容显示到 一个 textarea 中,然后修改内容在写入

7. PHP修改文本文件内容怎么实现

<?php//从文件中读取$path="1.txt";$fp=file($path);$arr=array();foreach($fpas&$line){$data=explode("=",$line);if(count($data)>1){$arr[]=array($data[0]=>$data[1]);}else{$arr[]=$line;}}//假设要修改ProctType为10setValue("ProctType","10",$arr);//var_mp($arr);//重新保存到文件$fp=fopen("2.txt","w");foreach($arras$row){if(is_array($row)){foreach($rowas$key=>$r){fwrite($fp,$key."=".$r);}}else{fwrite($fp,$row);}}fclose($fp);functionsetValue($name,$value,&$arr){foreach($arras$key=>$row){if(is_array($row)&&isset($row[$name])){$arr[$key][$name]=$value;//修改后记得加上换行$arr[$key][$name]=$arr[$key][$name]."";}}}?>

8. php修改文件内容问题。

;?> 追问: 我是想张三 永久换成李四,就是修改那个文件! 回答: 永久需要持久回化,就得用答后端数据库。 追问: 不要吧,。别人直接在网页里面编辑某个文件后保存就行了! 回答: 文件存放文本还行,如果你的数据有一定的格式的话,就需要数据库或者XML了。 追问: 那个文件是config.php里面只包括<?php$name="asd";$sex="1";$age="20";?>都是这样的样式!,能通过网页直接修改里面的值么? 回答: 一般这种配置文件是这样的,你可以在引入这个文件的后面,引入一个文件改变上面的变量,后面引入的这个文件就把前面的文件中的内容覆盖了,那些框架就是这样做的。

9. php 怎么修改txt文本

PHP有两种方法读写文件,方法一、file、file_get_contents、file_put_contents三个函数整体读写文本,适合文本文件不太大的情况。两个函数的的典型应用是:

$text=file_get_contents('a.txt');//把文本文件的所有内容取到字符串变量$text里面

$arr=file('a.txt');//把文本文件的所有内容,取到数组$arr里面,$arr[0]就是第一行,以此类推

$arr[1]='abc';//或者通过其它途径修改变量值

$text=implode($arr);//把数组连接为字符串

file_put_contents('a.txt', $text);//把字符串变量的内容写入到文本文件里面。

方法二、使用fopen、fgets、fputs、fclose函数读写文件,可以应付特大文件的修改。文件的修改一般方法是新建立一个文件,把源文件全部扫描一遍,遇到需要的部分进行处理,最后删除源文件,更名新文件。例如下面的代码把a.txt里面的abc修改为def:

$fp1=fopen('a.txt','r');$fp2=fopen('a.tmp','w');while(!feof($fp1)){$line=fgets($fp1);$line=str_replace('abc','def',$line);fputs($fp2,$line);}flcose($fp1);fclose($fp2);unlike('a.txt');rename('a.tmp','a.txt');

10. 网页(html,php)如何动态修改文件内容

<?php//获取文本内容$content = file_get_contents("/website/aa.txt");//查找localhsot,替换成您的IP地址$str = str_replace("localhost","127.0.0.1",$content);//以读写模式打开aa.txt文件$file = fopen("/website/aa.txt","r+");//将替换后的内容写入aa.txt文件中fwrite($file,$str);//关闭文件fclose($file);?>

未经允许不得转载:山九号 » php修改文件内容|php修改php文件内容

赞 (0)