Home > Backend Development > PHP Tutorial > PHP fgets reads strings line by line and explode splits strings into arrays_PHP tutorial

PHP fgets reads strings line by line and explode splits strings into arrays_PHP tutorial

WBOY
Release: 2016-07-13 10:07:57
Original
984 people have browsed it

PHP fgets reads strings line by line and explode splits strings into arrays

file.txt

Type|Directory|File|Size|Creation time

File|C:\Program Files\Java\jre7\lib\zi\America\Indiana|Vevay|724|2014-10-13 09:36 
File|C:\Program Files\Java\jre7\lib\zi\America\Indiana|Vincennes|884|2014-10-13 09:36
File|C:\Program Files\Java\jre7\lib\zi\America\Indiana|Winamac|932|2014-10-13 09:36 
Directory|C:\Program Files\Java\jre7\lib\zi\America|Indiana|0|2014-10-13 09:36
Copy after login

readfile.php

<?php
	$f= fopen("file.txt","r");
	while (!feof($f))
	{
  		$line = fgets($f);
  		echo $line,"<br />";
	
		$str = explode("|",$line);
		//print_r($str);
	
		$file_type =  $str [0];
		$dir_name =  $str [1];
		$file_name =  $str [2];
		$file_size =  $str [3];
		$create_time =  $str [4];

		echo $file_type;
		echo "</br>";
	
		echo $dir_name;
		echo "</br>";
	
		echo $file_name;
		echo "</br>";

		echo $file_size;
		echo "</br>";
	
		echo $create_time;
		echo "</br>";

	}
	fclose($f);
	
?>
Copy after login

Result display

File|C:\Program Files\Java\jre7\lib\zi\America\Indiana|Vevay|724|2014-10-13 09:36 
File
C:\Program Files\Java\jre7\lib\zi\America\Indiana
Vevay
724
2014-10-13 09:36 
File|C:\Program Files\Java\jre7\lib\zi\America\Indiana|Vincennes|884|2014-10-13 09:36 
File
C:\Program Files\Java\jre7\lib\zi\America\Indiana
Vincennes
884
2014-10-13 09:36 
File|C:\Program Files\Java\jre7\lib\zi\America\Indiana|Winamac|932|2014-10-13 09:36 
File
C:\Program Files\Java\jre7\lib\zi\America\Indiana
Winamac
932
2014-10-13 09:36 
Directory|C:\Program Files\Java\jre7\lib\zi\America|Indiana|0|2014-10-13 09:36 
Directory
C:\Program Files\Java\jre7\lib\zi\America
Indiana
0
2014-10-13 09:36 
Copy after login


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/953329.htmlTechArticlePHP fgets reads the string line by line and explodes the string into an array file.txt type | directory | file |Size|Creation time File|C:Program FilesJavajre7libziAmericaIndiana|Veva...
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