Php 试图从多个文本文件中获取信息以显示在网站表中
我一直在试图找出如何从单个文件夹中的多个文本文件中提取信息,以便整齐地显示在网页的表格中,我需要清理和显示大约3行。文本文件的数量会有所不同 我需要从文本文件中提取的行以以下开头:Php 试图从多个文本文件中获取信息以显示在网站表中,php,foreach,html-table,text-files,Php,Foreach,Html Table,Text Files,我一直在试图找出如何从单个文件夹中的多个文本文件中提取信息,以便整齐地显示在网页的表格中,我需要清理和显示大约3行。文本文件的数量会有所不同 我需要从文本文件中提取的行以以下开头: name = category = source = 文本文件中将包含的内容的一些示例: 示例1.conf: name = unique name1 information I do not want more information I do not want category = unique cat
name =
category =
source =
文本文件中将包含的内容的一些示例:
示例1.conf:
name = unique name1
information I do not want
more information I do not want
category = unique category1
source = https://unique-source1.com
a lot of information I do not want
示例2.conf
information I do not want
name = unique name2
more information I do not want
category = unique category2
source = https://unique-source2.com
a lot of information I do not want
示例3.conf
name = unique name3
category = unique category3
information I do not want
source = https://unique-source3.com
a lot of information I do not want
他们需要展示这样的东西:
____________________________________________
| Name | Category | Source |
|------------|----------------|--------------|
|unique name1|unique category1|unique source1|
|unique name2|unique category2|unique source2|
|unique name3|unique category3|unique source3|
|unique name4|unique category4|unique source4|
|unique name5|unique category5|unique source5|
|unique name6|unique category6|unique source6|
--------------------------------------------
此代码用于一次抓取一个项目,并将其列在一行中:
<?php
$path_to_check = '';
$needle = 'name = ';
foreach(glob($path_to_check . '*.conf') as $filename)
{
foreach(file($filename) as $fli=>$fl)
{
if(strpos($fl, $needle)!==false)
{
echo preg_replace(array('/name = /'),
'', $fl,);
echo "<br />";
}
}
}
?>
为了让它抓取其他项目,我需要更改$needle='name=
';和('/name=
/')以匹配我需要的名称
我原以为我的问题中有足够的信息来解释我需要什么,但正如一些人指出的那样,看起来我没有,所以我更新了帖子,以防其他人需要帮助。希望它能解决他们的问题。我让它起作用了,也许有一种更干净的方法,但这至少起作用了:
<head>
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg .tg-73oq{border-color:#000000;text-align:left;vertical-align:top}
</style>
</head>
<body>
<table class="tg">
<tr>
<th class="tg-73oq">Name</th>
<th class="tg-73oq">Category</th>
<th class="tg-73oq">Source</th>
</tr>
<?php
$path_to_check = '';
$needle1 = 'name = ';
$needle2 = 'category = ';
$needle3 = 'source = ';
foreach(glob($path_to_check . '*.conf') as $filename)
{
foreach(file($filename) as $fli=>$fl)
{
if(strpos($fl, $needle1)!==false)
{
echo "<tr>";
echo '<td class="tg-73oq">'.preg_replace(array('/name = /','/\(RAM \>.+\)/'),'', $fl,)."</td>";
}
}
foreach(file($filename) as $fli=>$f2)
{
if(strpos($f2, $needle2)!==false)
{
echo '<td class="tg-73oq">'.preg_replace(array('/category = /'),'', $f2,)."</td>";
}
}
foreach(file($filename) as $fli=>$f3)
{
if(strpos($f3, $needle3)!==false)
{
echo '<td class="tg-73oq">'.'<a href='.preg_replace(array('/source = /'),'',$f3,).'>'.'Source'.'</a>'."</td>";
echo "</tr>";
}
}
}
?>
</table>
</body>
.tg{边框折叠:折叠;边框间距:0;}
.tg td{字体系列:Arial,无衬线;字体大小:14px;填充:10px 5px;边框样式:实心;边框宽度:1px;溢出:隐藏;分词:正常;边框颜色:黑色;}
.tg th{字体系列:Arial,无衬线;字体大小:14px;字体大小:正常;填充:10px 5px;边框样式:实心;边框宽度:1px;溢出:隐藏;分词:正常;边框颜色:黑色;}
.tg.tg-73oq{边框颜色:#000000;文本对齐:左;垂直对齐:上}
名称
类别
来源
错误是什么或您想要什么,请详细说明。到目前为止,我尝试添加的所有表都会破坏它。至于我想要的,我需要从一个表中的多个文本文件中获得多个排序结果。你能说得更具体一些吗……你的评论没有明确任何内容。我真的不知道如何比主要帖子中的内容更具体,它准确地解释了我需要的内容。添加一个示例文本文件(仅几行)以及您试图将这些行划分为不同部分的代码。