PHP method example of reading txt file line by line

小云云
Release: 2023-03-21 19:24:01
Original
11667 people have browsed it

In PHP, you can use Excel files to import and export file data, making the data more intuitive. However, operating Excel files in projects usually requires relying on PHPExcel class files, and the execution efficiency is not as good as txt text files. If the number of data columns is relatively large and you need to perform statistics on the exported results, you can use Excel. If the number of columns is small and you do not need to process the results too much, you can use a txt file.

PHP implements the import and export of Excel data, please refer to the article: Use PHPExcel to implement the import and export of Excel files
PHP generates txt files, see for details Article: PHP generates txt file title and content

Here, simply implement the following PHP to read the txt file line by line, and read out the contents of the txt file The content and converted into an array we are familiar with:

/*
 * 逐行读取TXT文件 
 */
function getTxtcontent($txtfile){
	$file = @fopen($txtfile,'r');
	$content = array();
	if(!$file){
		return 'file open fail';
	}else{
		$i = 0;
		while (!feof($file)){
			$content[$i] = mb_convert_encoding(fgets($file),"UTF-8","GBK,ASCII,ANSI,UTF-8");
			$i++ ;
		}
		fclose($file);
		$content = array_filter($content); //数组去空
	}

	return $content;
}
Copy after login

TXT text data is as follows:


The reading result is as follows:


Related recommendations:

Ajax reads txt and displays the txt content in pages

How to delete specified lines of txt files in php and read txt document data line by line

php reads txt files and inserts data into the database

The above is the detailed content of PHP method example of reading txt file line by line. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!