每個主題對應一個選單檔(如 TradeShow.txt),格式:
故事編號^標題^圖片URL^摘要
驅動程式讀取檔案到陣列 $stories,$stories[0] 為最新文章。
for ($i = 0; $i < $numstories; $i++) {
$storyinfo = split("\^", $stories[$i]);
$storynum = $storyinfo[0]; // 如 33
$storydesc = $storyinfo[1]; // 標題
$storyimg = $storyinfo[2]; // 圖片 URL
$dtext = $storyinfo[3]; // 摘要
$url = "<a href=\"index.php?topic=$topic&story=$storynum\">$storydesc</a><br>";
if ($i < 10) {
$url = "<h2>$url</h2>";
if ($storyimg != "") {
$align = ($i % 2 == 1) ? "right" : "left";
$url = "<p><img align=$align src=\"$storyimg\"></p>\n$url";
}
$url = "<hr>$url";
} else {
$url = "<h3>$url</h3>";
}
echo $url;
echo "<p>$dtext</p>\n";
}
htmlspecialchars() 轉義所有標籤,再放行 <b><i><u><a><img> 等安全標籤(以正則還原)。33.txt。33^標題^圖片^摘要。// 先全轉義
$body = htmlspecialchars($body);
// 再還原允許標籤
$allowed = array('b','i','u','a','img','br','p');
foreach ($allowed as $tag) {
$body = preg_replace("/<\/?$tag( [^>]*)?>/i", "<$tag$1>", $body);
}
寫入選單檔時使用 flock() 獨佔鎖,避免多用戶同時投稿導致資料遺失。