php - 将 foreach 的值返回到表中
问题描述
我有一个包含不同文件的文件夹和 3 个文件夹(test1、test2 和 test3) 在这 3 个文件夹中,有 3 个带有标题的 HTML 文件(index1.html、index2.html、index3.html)
我想将 foreach 的结果显示到“标题网站”中,但我不知道
我包括我的脚本和图像以便更好地理解。
<body>
<div id="container">
<h1>Home Page</h1>
<table class="table table-hover ">
<thead>
<tr>
<th>Filename</th>
<th>Date</th>
<th>Title Web Site</th>
</tr>
</thead>
<tbody>
<?php
// Opens directory
$myDirectory=opendir(".");
// Gets each entry
while($entryName=readdir($myDirectory)) {
$dirArray[]=$entryName;
}
// Finds extensions of files
function findexts ($filename) {
$filename=strtolower($filename);
$exts=explode(" ", $filename);
$n=count($exts)-1;
$exts=$exts[$n];
return $exts;
}
// Closes directory
closedir($myDirectory);
// Counts elements in array
$indexCount=count($dirArray);
$fileList = glob('test?/*.html');
$array = array();
foreach($fileList as $file_title){
$html = file_get_contents($file_title);
preg_match("/<title>([^<]*)<\/title>/im", $html, $matches);
array_push($array, $matches[1]);
}
var_dump($array);
// Loops through the array of files
for($index=0; $index < $indexCount; $index++) {
// Allows ./?hidden to show hidden files
if($_SERVER['QUERY_STRING']=="hidden")
{$hide="";
$ahref="./";
$atext="Hide";}
else
{$hide=".";
$ahref="./?hidden";
$atext="Show";}
if(substr("$dirArray[$index]", 0, 1) != $hide) {
// Gets File Names
$name=$dirArray[$index];
$namehref=$dirArray[$index];
// Gets Date Modified Data
$modtime=date("Y-m-d H:i", filemtime($dirArray[$index]));
// Separates directories
if(is_dir($dirArray[$index])) {
$class="dir";
} else {
$class="file";
}
print("
<tr class='$class'>
<td><a href='./$namehref'>$name</a></td>
<td><a href='./$namehref'>$modtime</a></td>
</tr>");
}
}
?>
</tbody>
</table>
</div>
</body>
解决方案
推荐阅读
- angularjs - MongoDB - InsertOne() 获取 InsertID
- python - 将数据转储到 pandas df
- java-8 - stream-API:用流替换 for-each 循环(内部有昂贵的操作调用)
- wso2 - WSO2 IE 6.3.0:将 H2 更改为 MySQL 的问题
- javascript - JS 中的 getDirection() 确定 Dynamics 365 中 BPF 中的方向
- r - 折旧率为1%的累计金额
- angular - “用户”类型上不存在属性“状态”
- html - 为什么当我更改导航栏高度时,引导导航栏下拉菜单不会将内容移动到下方?
- python - 使用 python paho.mqqt 从 docker 容器通过 ssl 发送数据
- regex - 日期和 3 字母代码的正则表达式