当前位置:网站首页>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!
边栏推荐
- Short video SEO search operation customer acquisition system function introduction
- Ansible中的任务执行控制
- 业务测试如何避免漏测 ?
- 短视频seo搜索优化主要内容
- bgp aggregation reflector federation experiment
- 短视频SEO优化教程 自媒体SEO优化技巧方法
- Deliver cloud-native microservices applications with Zadig
- 当奈飞的NFT忘记了Web2的业务安全
- 解析正则表达式的底层实现原理
- [21-Day Learning Challenge] A small summary of sequential search and binary search
猜你喜欢

TCL: Pin Constraints Using the tcl Scripting Language in Quartus

【加密周报】经济衰退在加息气氛中蔓延 美联储“放手一搏”?盘点上周加密市场发生的重大事件

什么是低代码(Low-Code)?低代码适用于哪些场景?

零基础如何学习单片机,一位入门者的进阶路径,可参考

回顾历史5次经济衰退时期:这一次可能会有何不同?

Deliver cloud-native microservices applications with Zadig

security cross-domain configuration

为什么要使用MQ消息中间件?这几个问题必须拿下

当奈飞的NFT忘记了Web2的业务安全

Keepalived 高可用的三种路由方案
随机推荐
What is the function of the JSP Taglib directive?
信息物理系统状态估计与传感器攻击检测
辨析内存函数memset、memcmp、memmove以及memcpy
els 方块变形
bgp 聚合 反射器 联邦实验
Arduino 基础语法
Multi-feature fusion face detection based on attention mechanism
Deliver cloud-native microservices applications with Zadig
已知中序遍历数组和先序遍历数组,返回后序遗历数组
单片机遥控开关系统设计(结构原理、电路、程序)
TCL: Pin Constraints Using the tcl Scripting Language in Quartus
els 长条变形
Study Notes: The Return of Machine Learning
els strip deformation
众筹DAO“枯萎”的缩影:曾拍下《沙丘》未出版手稿的Spice DAO解散
After reshipment tencent greetings to monitor if the corresponding service does not exist by sc. Exe command to add services
业务测试如何避免漏测 ?
【解决】win10下emqx启动报错Unable to load emulator DLL、node.db_role = EMQX_NODE__DB_ROLE = core
[头条]笔试题——最小栈
Industrial control network intrusion detection based on automatic optimization of hyperparameters