如何将一个文本内容通过PHP 以表格的方式输入到页面上

时间:2023-12-26 14:49:13

如何将一个文本内容通过PHP 以表格的方式输入到页面上

<?php
//读取文本内容
$contents = file_get_contents("names.txt");
//分割字符串
$lines = explode("\n",trim($contents));
foreach ($lines as $row) {
$data[]=explode('|',$row);
} ?>
<!-- 将文本内容放入表格中 -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>人员名单</title>
</head>
<body>
<h1>人员名单</h1>
<table>
<thead>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>邮箱</th>
<th>网址</th>
<tbody>
<?php foreach ($data as $row): ?>
<tr>
<?php foreach ($row as $col): ?>
<?php $col=trim($col) ?>
<?php if (strpos($col, 'http://')===0): ?>
<td><a href="<?php echo strtolower($col)?>"><?php echo substr(strtolower($col),7); ?></a></td>
<?php else: ?>
<td><?php echo $col;?></td>
<?php endif ?>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</tbody>
</thead>
</table>
</body>
</html>

  具体看代码 @不懂的留言!