如何分解包含.txt文件中的内容?

问题描述:

如何分解PHP中包含的.txt文件的内容?我知道如何通过创建数组和使用爆炸函数来通过.php包含文件来实现。但是对于一个文本文件,我不能使用数组。我试图让我的网页显示此:如何分解包含.txt文件中的内容?

John Lee 
University of Oregon 
Sophmore 

Anna Smith 
Harvard 
Senior 

Thomas Stout 
University of California Irvine 
Junior 

这里是我的代码:

.txt文件:

John Lee~University of Oregon~Sophmore 
Anna Smith~Harvard~Senior 
Thomas Stout~University of California Irvine~Junior 

student.php文件:

<?php 
include ("student.php)"; 
#??? 
?> 

为什么不将.txt文件加载到变量中,然后用适当的正则表达式运行preg_split()?在你万一你想在〜字符和换行符拆分,所以它会像这样:

$input = file_get_contents('students.txt'); 
$output = preg_split("/(~|\n)/", $input); 

然后,它的那么容易,因为:

foreach($output as $line) 
    echo $line . '<br/>'; 

Example