How to modify file content in php

藏色散人
Release: 2023-03-03 08:30:01
Original
5955 people have browsed it

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 . 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);
?>
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!