Home  >  Article  >  Backend Development  >  How to modify file content in php

How to modify file content in php

藏色散人
藏色散人Original
2020-07-24 10:28:435957browse

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 .

How to modify file content in php

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(&#39;1.php&#39;);
$content = str_replace(&#39;hello world&#39;,&#39;hello baidu&#39;,$content);
file_put_contents(&#39;1.php&#39;,$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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn