) om sökningen öppnas upp. Jag är fullt medveten om att koden kan skrivas bättre men i dagsläget finns inga planer på att förfina den.
Kod:
<?php
include("simple_html_dom.php");
// Get data
$items = extractData("cached.htm");
// Write data
echo writeDocument($items);
//----------------------------------------------------------
// Function - write xml body
//----------------------------------------------------------
function writeDocument($items)
{
$output =
"<?xml version='1.0' encoding='ISO-8859-1'?>
<rss version='2.0'>
<channel>
<title>Sharps.se - Soulman posts</title>
<link>https://www.sharps.se</link>
<description>Sharps.se RSS feed</description>
<language>en-us</language>
<copyright>Copyright (C) 2012 persa</copyright>
$items</channel>
<rss>";
return $output;
}
//----------------------------------------------------------
// Function - extract data from url
//----------------------------------------------------------
function extractData($url)
{
// Get current timestamp
$time_unix = time();
$day = 86400;
// Set the output string
$items = "";
// Create DOM from URL or file
$html = file_get_html($url);
// Parse the file and find all <table ... id="post???">
foreach($html->find("table[id^=post]") as $element)
{
// Get <td>-element
$td = $element->find('td', 0);
// Filter out garbage
$td->find('span', 0)->outertext = "";
$td->find('img', 0)->outertext = "";
// Get date and time
@list($date, $time) = explode(",", $td->innertext);
$date = trim($date);
$time = trim($time);
// Set correct date for today and yesterday
if ($date == "idag") $date = date("Y-m-d", $time_unix);
if ($date == "igår") $date = date("Y-m-d", $time_unix - $day);
$date = date("D, d M Y H:i:s O", strtotime("$date $time"));
// Get the correct div with the containing description
$div = $element->find('div[class=alt2]', 0)->find('em', 0);
// Remove images
$div->find('img', 0)->outertext = "";
// Get the link and title
$link = "https://www.sharps.se/forums/" . $div->find('a', 0)->href;
$title = str_replace("\n", " ", $div->find('a', 0)->innertext);
// Filter out garbage
$div->find('a', 0)->outertext = "";
$div->find('br', 0)->outertext = "";
// Get the description
$description = trim(str_replace("<br />", " ", $div->innertext));
$description = str_replace("\n", " ", $description);
// Write RSS item
$items .=
" <item>
<title>$title</title>
<link>$link</link>
<description>$description</description>
<pubDate>$date</pubDate>
</item>\n";
}
return $items;
}
?>