如何从PHP中的include .txt文件中分解内容?我知道如何通过创建数组和使用explode函数来使用.php包含文件。但是使用文本文件,我无法使用数组。我正试着让我的页面显示出来:
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)";
#???
?>
答案 0 :(得分:0)
为什么不将.txt
文件加载到变量中,然后使用适当的Regex运行preg_split()
?在你的情况下你想要分割〜字符和换行符,所以它会是这样的:
$input = file_get_contents('students.txt');
$output = preg_split( "/(~|\n)/", $input );
然后它就像:
一样简单foreach( $output as $line )
echo $line . '<br/>';