php读取文件内容:
-----第一种方法-----fread()--------
1 2 3 4 5 6 7 8 |
|
--------第二种方法------------
1 2 3 4 5 6 7 8 |
|
-----第三种方法------------
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
-------第四种方法--------------
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
----第五种方法--------------------
1 2 3 4 5 6 7 8 9 10 11 12 |
|
如图将上述的文本内容打印到网页上,代码如下
<?php
$file_path="个人数据.txt";//此处可以将自己设置的文本的路径添加进来
$file_arr = file($file_path);
$file_mes[] = array();
for($i=0;$i<count($file_arr);$i++){//逐行读取文件内容
$file_mes[$i] = $file_arr[$i] ;//将读取到的内容放入到所定义的数组中
$data[]=explode('|', $file_mes[$i]);//按照|来分割字符
}
//var_dump($data);用来调试输出的
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<table border="1px solid red" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>邮箱</th>
<th>网址</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $line): ?><!-- 遍历行 -->
<tr>
<?php foreach ($line as $col): ?><!-- 遍历行中的数组 -->
<td><?php echo trim($col); ?> </td><!-- trim函数是删除字符串两边的空格的 -->
<?php endforeach ?>
</tr>
<?php endforeach ?>
</tbody>
</table>
</body>
</html>
网页上的打印效果如下