php 读入txt 字符串,PHP读取文本文件内容的五种方式
php读取文件内容的五种方式
分享下php读取文件内容的五种方法:好吧,写完后发现文件全部没有关闭。实际应用当中,请注意关闭 fclose($fp);
--
php读取文件内容:
-----第一种方法-----fread()--------
| 1 2 3 4 5 6 7 8 |
<?php$file_path = "test.txt";if(file_exists($file_path)){$fp = fopen($file_path,"r");$str = fread($fp,filesize($file_path));//指定读取大小,这里把整个文件内容读取出来echo $str = str_replace("rn","<br />",$str);}?> |
--------第二种方法------------
| 1 2 3 4 5 6 7 8 |
<?php$file_path = "test.txt";if(file_exists($file_path)){$str = file_get_contents($file_path);//将整个文件内容读入到一个字符串中$str = str_replace("rn","<br />",$str);echo $str;}?> |
-----第三种方法------------
| 1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php$file_path = "test.txt";if(file_exists($file_path)){$fp = fopen($file_path,"r");$str = "";$buffer = 1024;//每次读取 1024 字节while(!feof($fp)){//循环读取,直至读取完整个文件$str .= fread($fp,$buffer);} $str = str_replace("rn","<br />",$str);echo $str;}?> |
-------第四种方法--------------
| 1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php$file_path = "test.txt";if(file_exists($file_path)){$file_arr = file($file_path);for($i=0;$i<count($file_arr);$i++){//逐行读取文件内容echo $file_arr[$i]."<br />";}/*foreach($file_arr as $value){echo $value."<br />";}*/}?> |
----第五种方法--------------------
| 1 2 3 4 5 6 7 8 9 10 11 12 |
<?php$file_path = "test.txt";if(file_exists($file_path)){$fp = fopen($file_path,"r");$str ="";while(!feof($fp)){$str .= fgets($fp);//逐行读取。如果fgets不写length参数,默认是读取1k。}$str = str_replace("rn","<br />",$str);echo $str;}?> |
以上内容给大家分享了PHP读取文件内容的五种方式,希望大家喜欢。
本站其他内容推荐
1、kaleidoscope economics whey barleycorn liberalize Fiji upas capias explanation ashram
2、this is a test中文翻译,this is a test是什么意思,this is a test发音、用法及例句
3、strongbox是什么意思,strongbox中文翻译,strongbox发音、用法及例句
4、normally是什么意思,normally中文翻译,normally发音、用法及例句
5、riley英文名的寓意,riley是什么意思,riley中文翻译,riley发音、用法及例句
6、灼[ zhuó ],灼字的拼音,部首,意思,组词,成语,灼字的笔顺,笔画顺序怎么写
7、糡[ jiàng ],糡字的拼音,部首,繁体,糡字的意思
8、有学问的成语有哪些,形容有学问有才能的成语,四字成语,四字词语
9、fasciculus是什么意思,fasciculus中文翻译,fasciculus怎么读、发音、用法及例句
版权声明: 本站仅提供信息存储空间服务,旨在传递更多信息,不拥有所有权,不承担相关法律责任,不代表本网赞同其观点和对其真实性负责。如因作品内容、版权和其它问题需要同本网联系的,请发送邮件至 举报,一经查实,本站将立刻删除。






