Home >Backend Development >PHP Problem >How to modify the content of saved txt in php

How to modify the content of saved txt in php

PHPz
PHPzOriginal
2023-03-31 09:06:17830browse

PHP is a very popular server-side scripting language and one of the main tools for web application development. During PHP development, it is often necessary to process text files, such as TXT files. Therefore, this article will explain how to modify and save TXT files using PHP.

  1. Open TXT file

To open a TXT file in PHP, you need to use the file stream function fopen(). The function is used as follows:

$file = fopen("file.txt", "r");//打开一个 TXT 文件,以只读模式打开

The first parameter is the name and path of the file to be opened, and the second parameter is the access mode of the file. Common access modes are:

  • "r": read-only mode, reading from the file header
  • "r ": read-write mode, reading from the file header or Write
  • "w": Write-only mode, if the file does not exist, create it, delete and recreate it if it exists
  • "w": Read-write mode, if the file does not exist, create it, If it exists, delete and recreate it
  • "a": Append mode, if the file does not exist, create it, if it exists, append the content to the end of the file
  • "a": Read-write mode, if the file does not exist If it exists, create it, and if it exists, append the content to the end of the file

In this article, we use "r" mode to open the TXT file, the code is as follows:

$file = fopen("file.txt", "r+");
  1. Read TXT File content

After opening the file, we need to read the content from the file. You can use the fgets() function to read the file content line by line and the fread() function to read the file content by byte. This article uses the fgets() function to read. The code is as follows:

while(!feof($file)) {
    $line = fgets($file);
    echo $line."<br>";
}

Among them, the feof() function returns true when the end of the file is reached, and we use a while loop to read the entire file. The read data is stored in the $line variable and is output directly in this article.

  1. Modify the TXT file content

After reading the TXT file, we can modify it. The modification method is similar to the reading method. You can use the fputs() function to write content by rows and the fwrite() function to write content by bytes.

In order to demonstrate the process of modifying the file content, we add the "*" string to each line of the TXT file. The code is as follows:

//修改 TXT 文件内容
rewind($file);//回到文件开头
while(!feof($file)) {
    $line = fgets($file);
    $newLine = $line . "***";
    fwrite($file, $newLine);//将修改后的内容写回到文件
}

Among them, the rewind() function returns the file pointer to the beginning of the file and prepares for writing operations. After each line is read, save the new string to be written in the $newLine variable and use the fwrite() function to write the new string back to the file.

Note that the file pointer will move to the next readable and writable location after each fwrite() operation.

  1. Save the TXT file content

After modifying the file content, we need to save the new content to the file. You can use the fclose() function to close the file stream, or you can use the fflush() function to write the contents of the buffer area to a file and close the file stream.

The code is as follows:

fflush($file);//将缓存区的内容写入文件并关闭文件流

fflush() function writes the contents of the buffer area of ​​the file stream into the file. If the file stream is bidirectional, the input buffer area and the output buffer area are cleared at the same time. operation. At the same time, this function also closes the file stream.

  1. Complete sample code

Based on the above steps, we can get the complete sample code for modifying and saving TXT files in PHP as follows:

//打开 TXT 文件
$file = fopen("file.txt", "r+");

//读取 TXT 文件内容
while(!feof($file)) {
    $line = fgets($file);
    echo $line."<br>";
}

//修改 TXT 文件内容
rewind($file);
while(!feof($file)) {
    $line = fgets($file);
    $newLine = $line . "***";
    fwrite($file, $newLine);
}

//保存 TXT 文件内容
fflush($file);

//关闭 TXT 文件
fclose($file);
  1. Summary

Through this article, we learned how to use PHP to modify and save TXT files. Specifically, you need to use the fopen() function to open the file, use the fgets() function to read the file content, use the fwrite() function to write the written content back to the file, and use the fflush() function to save the file and close it after completing the modification. File stream. These operations are very useful for working with text files, allowing us to process text data more efficiently in PHP.

The above is the detailed content of How to modify the content of saved txt 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
Previous article:How to set PHP's PATHNext article:How to set PHP's PATH