在Web开发中,我们经常需要读取和处理文本文件。PHP作为一种流行的服务器端脚本语言,提供了多种方法来读取TXT文本内容。本文将介绍五种不同的PHP教程,帮助您学习如何使用PHP读取TXT文本内容。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 ); fclose( $fp ); } ?> |
第二种,用file_get_contents函数:
1 2 3 4 5 6 7 | <?php $file_path = "test.txt" ; if ( file_exists ( $file_path )){ $str = file_get_contents ( $file_path ); //将整个文件内容读入到一个字符串中 $str = str_replace ( "rn" , "<br>" , $str ); echo $str ; } ?> |
第三种,用fopen函数:
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 ; fclose( $fp ); } ?> |
第四种方法,使用file函数:
1 2 3 4 5 6 7 8 9 | <?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>" ; fclose( $file_arr ); } } ?> |
第五种,还是使用fopen函数:
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 ; fclose( $fp ); } ?> |
当然,开启资源后,记得使用fclose($fp);关闭一下,不然的话,会消耗服务器的资源。
方法补充
除了上文的方法,小编还为大家整理了其他一些PHP读取TXT文本的方法,希望对大家有所帮助
php读取文件内容的三种方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | //**************第一种读取方式***************************** 代码如下: header( "content-type:text/html;charset=utf-8" ); //告诉php预处理器将内容已utf8的格式传递给浏览器 //文件路径 $file_path = "text.txt" ; //判断是否有这个文件 if ( file_exists ( $file_path )){ if (fp= fopen (file_path, "a+" )){ //读取文件 conn= fread (fp, filesize ( $file_path )); //替换字符串 conn=strreplace( "rn" , "<br>" ,conn); echo $conn . "<br>" ; } else { echo "文件打不开" ; } } else { echo "没有这个文件" ; } fclose( $fp ); //*******************第二种读取方式*************************** 代码如下: header( "content-type:text/html;charset=utf-8" ); //文件路径 $file_path = "text.txt" ; conn=filegetcontents(file_path); conn=strreplace( "rn" , "<br>" ,filegetcontents(file_path)); echo $conn ; fclose( $fp ); //******************第三种读取方式,循环读取***************** 代码如下: header( "content-type:text/html;charset=utf-8" ); //文件路径 $file_path = "text.txt" ; //判断文件是否存在 if ( file_exists ( $file_path )){ //判断文件是否能打开 if (fp= fopen (file_path, "a+" )){ $buffer =1024; //边读边判断是否到了文件末尾 $str = "" ; while (! feof ( $fp )){ str.= fread (fp, $buffer ); } } else { echo "文件不能打开" ; } } else { echo "没有这个文件" ; } //替换字符 str=strreplace( "rn" , "<br>" ,str); echo $str ; fclose( $fp ); |
利用fopen,file,file_get_contents函数来实现读取文本文件内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | //fopen 读取文件实例,代码如下: $path = 'a.txt' ; $fp = fopen ( $file , "r" ); //以只读的方式打开文件 while (!( feof ( $fp ))) { $text = fgets ( $fp ); //读取文件的一行 echo $text ; } //file_get_contents读取文件,代码如下: if ( file_exists ( $path ) ) { $body = file_get_contents ( $path ); echo $body ; //输入文件内容 } else { echo "文件不存在 $path" ; } //开源代码phpfensi.com //读取文本文件,代码如下: $cbody = file( $path ); print_r( $cbody ); //因为file读取出来的文件是以数组形式保存的,所以用print_r输出。 |
到此这篇关于PHP读取TXT文本内容的五种实用方法小结的文章就介绍到这了,更多相关PHP读取TXT内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!