我有这个文本文件:

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
有帮助吗?

解决方案

  1. 读取文件并将每一行存储到一个数组中。
  2. 打开 <table> 标签
  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"

对于数组中的每一项,写入适当的内容 <tr> & <td> 标签填充了提取的数据。

更新:

<?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>"
?>

其他提示

这样的事情应该做的伎俩(没有测试的代码):

$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>';
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top