Home >Backend Development >PHP Problem >How to modify file content in php
php method to modify file content: first read the data through the "file_get_contents" function; then use the "str_replace" function to modify the file content; finally use the "file_put_contents" function to write the modified content into the file .
Recommended: "PHP Tutorial"
phpModify the content of the php file:
Divided into three steps: reading, modifying and writing.
Implementation process:
1. Assume the file name is 1.php, read the data through the file_get_contents() function, and implement the code effect as $content = file_get_contents('1.php');
2. Assume that the content of the 1.php file is 3178654659fabb875fdd3fcb5d370085. Now we need to replace the value of the variable $op with hello baidu. The implementation code is: $ content = str_replace('hello world','hello baidu',$content);
3. Write the variable $content into the 1.php file. The implementation method is: file_put_contents('1.php',$ content);
The specific example code is as follows:
<?php $content = file_get_contents('1.php'); $content = str_replace('hello world','hello baidu',$content); file_put_contents('1.php',$content); ?>
The final 1.php file content is: $op = 'hello baidu';
The above is the detailed content of How to modify file content in php. For more information, please follow other related articles on the PHP Chinese website!