使用 PHP 如何将文本文件连同链接一起输出到表格中

发布于 2024-08-14 04:47:50 字数 527 浏览 3 评论 0原文

我有这个文本文件:

Dec 04 20:15 Naruto 123
Dec 04 17:42 Naruto 98
Dec 04 16:19 D Gray Man 001
Dec 04 16:05 Bleach 128
Dec 04 12:13 50 x 50 44

我需要将内容输出到一个表中,其中日期和时间在其自己的列中,而平铺和章节在另一列中。

另外..我需要用必须采用以下格式的链接替换标题和章节:

<a href="/title/title chapter">title chapter</a>

为了澄清,文本文件中的标题是:

Naruto
Naruto
D Gray Man
Bleach
50 x 50

章节是后面的数字:

123
98
001
128
44

I have this text file:

Dec 04 20:15 Naruto 123
Dec 04 17:42 Naruto 98
Dec 04 16:19 D Gray Man 001
Dec 04 16:05 Bleach 128
Dec 04 12:13 50 x 50 44

I need to output the contents into a table with the Date and Time in its own column and the Tile and Chapter in another.

Also.. I need to replace the Title and Chapter with a link that must be formatted this way:

<a href="/title/title chapter">title chapter</a>

For clarification the titles in the text file are:

Naruto
Naruto
D Gray Man
Bleach
50 x 50

And the chapters are the numbers that follow:

123
98
001
128
44

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪异←思 2024-08-21 04:47:51

像这样的事情应该可以解决问题(尚未测试代码):

$data = Explode ( "\n", File_Get_Contents ( 'yourfile' ) );

foreach ( $data as $key => $line )
{
    $tmp = Explode ( ' ', $line );
    $month = $tmp[0];
    $day = $tmp[1];
    $time = $tmp[2];
    $numOfEntries = Count ( $tmp );
    $chapter = $tmp[$numOfEntries - 1];

    $title = '';
    for ( $i = 3; $i < $numOfEntries - 1; $i++ )
        $title .= $tmp[$i] . ' ';

    $title = Trim ( $title );

    $link = '<a href="/' . $title . '/' . $title . ' ' . $chapter . '">' . $title . ' ' . $chapter . '</a>';
}

Something like this should do the trick (haven't tested the code):

$data = Explode ( "\n", File_Get_Contents ( 'yourfile' ) );

foreach ( $data as $key => $line )
{
    $tmp = Explode ( ' ', $line );
    $month = $tmp[0];
    $day = $tmp[1];
    $time = $tmp[2];
    $numOfEntries = Count ( $tmp );
    $chapter = $tmp[$numOfEntries - 1];

    $title = '';
    for ( $i = 3; $i < $numOfEntries - 1; $i++ )
        $title .= $tmp[$i] . ' ';

    $title = Trim ( $title );

    $link = '<a href="/' . $title . '/' . $title . ' ' . $chapter . '">' . $title . ' ' . $chapter . '</a>';
}
给我一枪 2024-08-21 04:47:51
  1. 读取文件并将每一行存储到一个数组中。
  2. 打开 标签

  3. 循环遍历数组并使用正则表达式提取日期/时间/标题/章节。

这是开始的正则表达式 - 您可能需要对其进行一些修改以满足您的需求:

/^([a-zA-Z]{3}\s\d{2})\s(\d{2}:\d{2})\s(.+?)\s(\d+)\s*?$/
//$1 contains date : "Dec 04"
//$2 contains time : "20:15"
//$3 contains the title : "Naruto"
//$4 contains the chapter : "123"

对于数组中的每个项目,编写适当的 & 标签填充了提取的数据。

更新:

<?php
$filedata = "Dec 04 20:15 Naruto 123
Dec 04 17:42 Naruto 98
Dec 04 16:19 D Gray Man 001
Dec 04 16:05 Bleach 128
Dec 04 12:13 50 x 50 44";

$lines = explode("\n", $filedata);

echo "<table border=\"1\">";

foreach($lines as $line)
{
    echo "<tr>";
    preg_match("/^([a-zA-Z]{3}\s\d{2}\s\d{2}:\d{2})\s(.+?)\s(\d+)\s*?$/", $line, $matches);
    echo "<td>$matches[1]</td>";
    echo "<td><a href=\"/$matches[2]/$matches[2] $matches[3]\">$matches[2] $matches[3]</a></td>";
    echo "</tr>";
}
echo "</table>"
?>
  1. Read the file and store each line into an array.
  2. Open <table> tag
  3. Loop through the array and extract the date/time/title/chapter using regex.

Here is the regex to start with - you might want to modify it a little to meet your needs:

/^([a-zA-Z]{3}\s\d{2})\s(\d{2}:\d{2})\s(.+?)\s(\d+)\s*?$/
//$1 contains date : "Dec 04"
//$2 contains time : "20:15"
//$3 contains the title : "Naruto"
//$4 contains the chapter : "123"

For each item in the array, write appropriate <tr> & <td> tags filled with the extracted data.

Update:

<?php
$filedata = "Dec 04 20:15 Naruto 123
Dec 04 17:42 Naruto 98
Dec 04 16:19 D Gray Man 001
Dec 04 16:05 Bleach 128
Dec 04 12:13 50 x 50 44";

$lines = explode("\n", $filedata);

echo "<table border=\"1\">";

foreach($lines as $line)
{
    echo "<tr>";
    preg_match("/^([a-zA-Z]{3}\s\d{2}\s\d{2}:\d{2})\s(.+?)\s(\d+)\s*?$/", $line, $matches);
    echo "<td>$matches[1]</td>";
    echo "<td><a href=\"/$matches[2]/$matches[2] $matches[3]\">$matches[2] $matches[3]</a></td>";
    echo "</tr>";
}
echo "</table>"
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文