码迷,mamicode.com
首页 > 编程语言 > 详细

PHP fgets按行读取字符串和explode分割字符串为数组

时间:2015-01-14 15:34:06      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

file.txt

 类型|所在目录|文件|大小|创建时间 

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

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);
	
?>

结果显示

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 


PHP fgets按行读取字符串和explode分割字符串为数组

标签:

原文地址:http://blog.csdn.net/sysprogram/article/details/42710557

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!