当前位置:网站首页>PHP to read data from TXT file

PHP to read data from TXT file

2022-08-02 00:32:00 Yisuyun

How PHP reads data from a txt file

This article mainly introduces "the method of reading data from txt file in PHP". In daily operation, I believe that many people have doubts about the method of reading data from txt file in PHP.The editor has checked various materials and sorted out simple and easy-to-use operation methods. I hope it will help you to answer your doubts about "how to read data from txt files in PHP"!Next, please follow the editor to learn together!

How PHP reads data from a txt file

One, open/close the file

1. When operating a file, first open the file, use the fopen() function to open the file, the syntax is:

fopen(filename,mode,include_path,context);

How PHP reads data from txt file

2. After the file operation is completed, the file should be closed, use the function fclose();

Example:

How PHP reads data from a txt file

Second, read and write files

1, read the entire file

There are three functions that can be used, namely: readfile() function, file() function, file_get_contents() function.

The

readfile() function, which reads a file and writes it to the output buffer, returns false if an error occurs.

The

file() function stores the contents of the file into an array by line, including newlines, and returns false if it fails.

The

file_get_contents() function reads the file content into a string. If there are offset and maxlen parameters, it will start reading the content of length maxlen at the position specified by the parameter offset, and returns false if it fails.

Example:

How PHP reads data from txt file

The running result is shown in the figure:

How PHP reads data from a txt file

2, read a line of data

The fgets() function and the fgetss() function both read a row of data.

fgets() function:

string fgets( resource $handle [, int $length ] );

$handle is the opened file, $length is the length of the data to be read.

fgetss() function:

string fgetss ( resource $handle [, int $length [, string $allowable_tags ]] );

The fgetss() function is a variant of the fgets() function and is the same as fgets() except that the fgetss() function strips any HTML and PHP tags from the read text.

3, read a character

When searching and replacing a certain character, it is necessary to read a certain character in a targeted manner, and use the function: fgetc();

How PHP reads data from txt file

4, read a string of any length

Read data of specified length from the file, use the function fread();

How PHP reads data from a txt file

At this point, the study on "the method of reading data from txt file in PHP" is over, and I hope to solve your doubts.The combination of theory and practice can better help everyone learn, go try it!If you want to continue to learn more relevant knowledge, please continue to pay attention to the Yisuyun website, and the editor will continue to work hard to bring you more useful articles!

原网站

版权声明
本文为[Yisuyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/214/202208020000495845.html