2009-12-07 33 views
0

我有這個文本文件一起:使用PHP你怎麼輸出一個文本文件到表的鏈接

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 
+0

你想怎麼的鏈接,是'「/火影忍者/火影忍者123 「'?檢查我的更新。 – Amarghosh 2009-12-07 08:03:04

+0

是的:D謝謝! – D3V1NE 2009-12-07 08:09:19

回答

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

謝謝你:D它的作品! – D3V1NE 2009-12-07 08:07:19

1

這樣的事情應該做的伎倆(沒有測試的代碼):

$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>'; 
} 
+0

反正有空留空間嗎?它輸出沒有空格。 view here: http://xennetworks.com/output5.php – D3V1NE 2009-12-07 07:58:03

+0

您的聲明有效,但正如我之前所述。有沒有辦法保持空間? 它取出標題中的空格。 例如: D灰色的男人變成===> DGrayMan – D3V1NE 2009-12-07 08:08:26

+0

看到我更改的代碼... – 2009-12-07 08:13:26

相關問題