php如何修改文件内容,文件,内容,函数,代码,变量
php如何修改文件内容
易采站长站,站长之家为您整理了php如何修改文件内容的相关内容。
php修改文件内容的方法:首先通过“file_get_contents”函数读出数据;然后使用“str_replace”函数修改文件内容;最后使用“file_put_contents”函数将修改后的内容写入文件中即可。
推荐:《PHP教程》
php修改php文件内容:
分三步:读取、修改、写入。
实现过程:
1、假设文件名为1.php,通过file_get_contents()函数读出数据,实现代码效果为 $content = file_get_contents('1.php');
2、假设1.php文件的内容为 <?php $op='hello world';?>,现在要把变量$op的值更换成 hello baidu,实现代码为:$content = str_replace('hello world','hello baidu',$content);
3、将变量$content 写入1.php文件,实现方式为:file_put_contents('1.php',$content);
具体实例代码如下:<?php $content = file_get_contents('1.php');$content = str_replace('hello world','hello baidu',$content);file_put_contents('1.php',$content);?>
最终1.php文件内容为:$op = 'hello baidu';以上就是关于对php如何修改文件内容的详细介绍。欢迎大家对php如何修改文件内容内容提出宝贵意见