Home >php教程 >php手册 >读取文本文件并显示在网页

读取文本文件并显示在网页

WBOY
WBOYOriginal
2016-05-25 16:57:331104browse
本文章来讲二种读取文本文件并显示在网页的php实例了,方法很简单,用fopen,加explode,for就实现了。
 代码如下 复制代码
$file = "./text.txt";
$text = fread(fopen($file,"r"),filesize($file));
$line = explode(" ",$text);
echo "";
    for($i = 0; $i  $txt = explode(" ",$line[$i]);
 echo "";
    }
echo "
".$txt[0]." ".$txt[1]." ".$txt[2]." ".$txt[3]."
";

//方法二

 代码如下 复制代码

$content = file_get_contents('test.txt');

$arr = explode(" ", $content);
echo "

";

foreach ($arr as $v) {
$tmp = explode("    ", $v);
echo "

";
echo "";
echo "";
echo "";
echo "";
echo "";
unset($tmp);
}

echo "

" . $tmp[0] . "" . $tmp[1] . "" . $tmp[2] . "" . $tmp[3] . "
";

//其实不有一个方法来实现上的实例,file函数哦,这个我也不说多了。查查就知道了,它读出内容直接保存到数据,就不用我们使用explode函数了。
/*
文本文本格式如下

读取一个文本文件内容如下
1       aaaa    bbba    ccca
2       aaaa    bbbb    cccb
3       aaaa    bbbc    cccc
3       aaaa    bbbc    cccc

 



Statement:
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