query('CREATE TABLE IF NOT EXISTS "articles" ("title" VARCHAR, "url" VARCHAR, "created" DATETIME DEFAULT CURRENT_TIMESTAMP)'); $sqlite->query('DELETE FROM "articles" WHERE "created" < DATETIME(\'now\', \'-3 days\')'); $select = $sqlite->prepare('SELECT "url" FROM "articles" WHERE "title" = ?'); $insert = $sqlite->prepare('INSERT INTO "articles" ("title", "url") VALUES (?, ?)'); // Process the original feed $xml = simplexml_load_file($feed); $ix = 0; $sqlite->exec('BEGIN'); while ($ix < count($xml->channel->item) ) { if ( filter($xml->channel->item[$ix]->title, $xml->channel->item[$ix]->link, $stopwords_title, $stopwords_url, $select, $insert) ) { unset($xml->channel->item[$ix]); } else { $ix++; } } $sqlite->exec('COMMIT'); $sqlite->close(); echo $xml->asXML(); // Filter article function filter($title, $url, $stopwords_title, $stopwords_url, $select, $insert) { // Filter by title foreach ($stopwords_title as &$stopword) { if (strpos($title, $stopword) !== false) { return true; } } // Filter by URL foreach ($stopwords_url as &$stopword) { if (strpos($url, $stopword) !== false) { return true; } } // Remove duplicates $select->bindValue(1, $title); $result = $select->execute(); $row = $result->fetchArray(SQLITE3_ASSOC); $result->finalize(); if ($row !== false) { if ($row['url'] != $url) { return true; } } else { $insert->bindValue(1, $title); $insert->bindValue(2, $url); $insert->execute(); } return false; } ?>