I have been trying to get this updated a bit, as the <img>
codes have been coming through and look pretty ugly on the site. I used the below code, but it is not working to convert <img>
to [img]
. Does anyone have any suggestions?
For some reason right now, it is putting the <img>
inside a[code]
box.
<?php
include("/home/treyb/SITE/rss/details.php");
require '/home/treyb/SITE/vendor/autoload.php';
// Array containing multiple RSS feed URLs
$site = array(
"https://www.batterypower.com/rss/current.xml", // Braves
"https://www.thefalcoholic.com/rss/current.xml", // Falcons
"https://www.dirtysouthsoccer.com/rss/current.xml", // United
"https://www.peachtreehoops.com/rss/current.xml", // Hawks
"https://www.atlantamusicguide.com/feed/" // Concerts
);
foreach ($site as $url) {
echo "\nProcessing RSS feed " . $url . "\n\n";
try {
// Initialize SimplePie for the current feed URL
$feed = new SimplePie();
$feed->enable_cache();
$feed->set_cache_location("/home/storage/cache");
$feed->set_feed_url($url);
$feed->init();
// Check for errors in SimplePie
if ($feed->error()) {
throw new Exception($feed->error());
}
// Process items in the feed
foreach ($feed->get_items() as $item) {
// Get description and content
$description = $item->get_description();
$content = $item->get_content();
// Convert img tags to BBCode in both description and content
$description = preg_replace('/<img[^>]+src="([^"]+)"[^>]+alt="([^"]+)"[^>]*>/i', '[img alt="$2"]$1[/img]', $description);
$content = preg_replace('/<img[^>]+src="([^"]+)"[^>]+alt="([^"]+)"[^>]*>/i', '[img alt="$2"]$1[/img]', $content);
// Combine description and content
$full_content = $description . "\n\n" . $content;
// Remove other unwanted tags and entities
$full_content = strip_tags(html_entity_decode($full_content), "<img>") . "\n";
$full_content .= "\n" . '[Link to original article](' . $item->get_link() . ')' . "\n\n";
// Get the title of the item
$subject = $item->get_title();
// Query the database for each item
$link = $item->get_link();
$stmt = $conn->prepare('SELECT url, seen FROM queue WHERE url = ?');
$stmt->bind_param('s', $link);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($checklink, $seen);
$stmt->fetch();
if (!$checklink || !$seen) {
echo "Checking " . $link . " \nThis does not exist...processing\n";
$seen = 1;
$stmt = $conn->prepare('INSERT INTO queue (url, title, seen) VALUES(?, ?, ?)');
$stmt->bind_param("ssi", $link, $subject, $seen);
$stmt->execute();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://SITE.COM/api/discussions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
'data' => array(
'type' => "discussions",
'attributes' => array(
'title' => $subject,
'content' => $full_content
),
'relationships' => array(
'tags' => array(
'data' => array(
array(
'type' => 'tags',
'id' => "85"
)
)
)
)
)
)));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
echo $result;
if ($result === false) {
throw new Exception('Curl error: ' . curl_error($ch));
}
curl_close($ch);
} else {
echo "Checking " . $checklink . " \nThis has already been processed...skipping\n";
}
}
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage() . "\n";
}
}
?>
PS, the script does handle multiple RSS Sites very well, and the [code]
box at least looks better than the alternative.