当前位置:网站首页>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!
边栏推荐
- bgp 聚合 反射器 联邦实验
- els 方块变形
- uni-app项目总结
- What is the function of the JSP out.println() method?
- 磁盘与文件系统管理
- Mean Consistency Tracking of Time-Varying Reference Inputs for Multi-Agent Systems with Communication Delays
- CRS management and maintenance
- 不就是个TCC分布式事务,有那么难吗?
- IO stream basics
- 【加密周报】经济衰退在加息气氛中蔓延 美联储“放手一搏”?盘点上周加密市场发生的重大事件
猜你喜欢
随机推荐
SphereEx Miao Liyao: Database Mesh R&D Practice under Cloud Native Architecture
Keepalived 高可用的三种路由方案
07-SDRAM: FIFO control module
Detailed explanation of JSP request object function
JSP out.print()和out.write()方法的不同之处
els 方块变形
接地气讲解TCP协议和网络程序设计
Using the "stack" fast computing -- reverse polish expression
如何发现新的潜力项目?工具推荐
微软电脑管家V2.1公测版正式发布
JSP built-in object out object function introduction
基于数据驱动的变电站巡检机器人自抗扰控制
Double queue implementation stack?Dual stack implementation queue?
Difference between JSP out.print() and out.write() methods
一篇永久摆脱Mysql时区错误问题,idea数据库可视化插件配置
面试必问的HashCode技术内幕
重装腾讯云云监控后如果对应服务不存在可通过sc.exe命令添加服务
460. LFU cache
Day11 shell脚本基础知识
Multi-feature fusion face detection based on attention mechanism