当前位置:网站首页>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!

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);

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

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.
Thereadfile() function, which reads a file and writes it to the output buffer, returns false if an error occurs.
Thefile() function stores the contents of the file into an array by line, including newlines, and returns false if it fails.
Thefile_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:

The running result is shown in the figure:

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();

4, read a string of any length
Read data of specified length from the file, use the function fread();

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!
边栏推荐
猜你喜欢
随机推荐
短视频seo搜索优化主要内容
JSP out.write()方法具有什么功能呢?
REST会消失吗?事件驱动架构如何搭建?
CRS 管理与维护
poker question
JSP how to obtain the path information in the request object?
Detailed explanation of JSP request object function
632. 最小区间
一文概览最实用的 DeFi 工具
具有通信时延的多自主体系统时变参考输入的平均一致性跟踪
go语言标准库fmt包怎么使用
When Netflix's NFTs Forget Web2 Business Security
【无标题】
Cyber-Physical System State Estimation and Sensor Attack Detection
双队列实现栈?双栈实现队列?
els 方块变形
Multi-feature fusion face detection based on attention mechanism
08-SDRAM:汇总
控制电机的几种控制电路原理图
Deliver cloud-native microservices applications with Zadig









