php:file()与file_get_contents():讲日志文件没行读为数组形式

Posted 穆晟铭

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php:file()与file_get_contents():讲日志文件没行读为数组形式相关的知识,希望对你有一定的参考价值。

file()与file_get_contents()一样,都是读取某文件的内容。file_get_contents()输出的是整个文件(不能读取TXT里的首行缩进和换行符)。 file() 函数把整个文件读入一个数组中。与 file_get_contents() 类似,不同的是 file() 将文件作为一个数组返回。数组中的每个单元都是文件中相应的一行,包括换行符在内。如果失败,则返回 false。

array(

[0] test1

[1]

[2]  test2

)

利用file()这个特性,我们可以用循环的方法把html的换行符和空格符放与文字连在一起。

welcome.txt 文件内容:

      test1

 

     test2

 


$welcomefile=file(‘welcome.txt‘); //返回数组的内容

foreach($welcomefile as $v){

        echo $v.‘<br>‘;

}

通过上面的代码,就能在.PHP文件中输出带&nbsp; 和 <br>的换行符,实现换行了。

以上是关于php:file()与file_get_contents():讲日志文件没行读为数组形式的主要内容,如果未能解决你的问题,请参考以下文章

php file_get_contents 绕过

php file_get_contents抓取

PHP file_get_contents() 函数

PHP file_get_contents 函数超时的几种解决方法

php 评估后的 PHP file_get_contents

PHP------------------------file_get_content获取不到页面信息