Home >Backend Development >PHP Problem >How to replace file content in php

How to replace file content in php

王林
王林Original
2020-07-29 15:43:154423browse

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.

How to replace file content in php

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:

How to replace file content in php

(Learning video recommendation: php video tutorial)

Code implementation:

How to replace file content in php

#The replaced file content is as shown below:

How to replace file content in php

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!

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