Home > Article > Backend Development > How to replace file content in php
php method to replace file content: first use the fopen() function to open the file, then use the fgets() function to read the file line by line, then use the str_replace() function to replace the file content, and finally use the fwrite() function Just write to the file.
Related function introduction:
(Recommended tutorial: php graphic tutorial)
feof () function checks whether the end of file (EOF) has been reached. Returns TRUE if an error occurs or the file pointer reaches the end of file (EOF), otherwise it returns FALSE.
fgets() function returns a line from an open file. The fgets() function will stop returning a new line when it reaches the specified length (length - 1), encounters a newline character, or reaches the end of file (EOF), whichever comes first. The function returns FALSE on failure.
str_replace() function replaces some characters in a string (case sensitive).
The file content before replacement is as shown below:
(Learning video recommendation: php video tutorial)
Code implementation:
#The replaced file content is as shown below:
The above is the detailed content of How to replace file content in php. For more information, please follow other related articles on the PHP Chinese website!