Help adding data to RSS feed field

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scriptacular
Forum Newbie
Posts: 6
Joined: Wed May 20, 2009 6:46 pm

Help adding data to RSS feed field

Post by scriptacular »

I'm trying to insert "<span class="full"> after the first 70 words, then </span> at the end of the text in the description field. I want to know if there is a way to do it for each item in the feed before it gets inserted into the database. If it's even possible?

ie from this;
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

to this;
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries,<span class="full> but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span>

Here's part of the code

Code: Select all

	/**
	 * Store a single item in the database.
	 * @param Lots, see definition. Everything is a string, exceptions:
	 * @param $dc_date integer representing the date/time as a unix timestamp.
	 */
	function saveRssItem($title, $link, $description, $dc_creator, $dc_date, $dc_subject) {
		$qry = "
			INSERT INTO items 
				(
				title, 
				link, 
				description, 
				dc_creator, 
				dc_date, 
				dc_subject
				) 
			VALUES
				(
				'".mysql_real_escape_string($title)."',
				'".mysql_real_escape_string($link)."',
				'".mysql_real_escape_string($description)."',
				'".mysql_real_escape_string($dc_creator)."',
				'".mysql_real_escape_string($dc_date)."',
				'".mysql_real_escape_string($dc_subject)."'
				)
		";

		$this->query($qry);
	}

}

/**
 * Quick'n'Dirty RSS reader and parser. Simply reads in the items in a feed. 
 */
class RSSParser {

	var $saveItems = array(
		"title"       => "string",
		"link"        => "string",
		"description" => "string",
		"dc:creator"  => "string",
		"dc:date"     => "date",
		"dc:subject"  => "string",
	);

	/**
	 * Read and parse a RSS feed which $url points at (may be an URL or local file)
	 * @param $url string containing the URL to the RSS feed. May be an URL or local file
	 */
	function RSSParser($url) {
		$this->read($url);
		$this->readItems();

	}
	
	/**
	 * Construct a new empty RSS item
	 * @return a new empty assoc. array representing an rss item 
	 */
	function newItem() {
		$retItem = array();

		foreach(array_keys($this->saveItems) as $key) {
			$retItem[$key] = "";
		}

		return($retItem);
	}

	/**
	 * Read a RSS file/url and make a big string of it so it can be parsed.
	 * @param $url string containing the URL to the RSS feed. May be an URL or local file
	 */
	function read($url) {
		$this->rssData = implode("", file($url));
	}

	/**
	 * Read <item> segments from the RSS file and stuff them in an array.
	 */
	function readItems() {
		if (!isset($this->rssData)) {
			return(-1);
		}

		$this->rssItems = array();

		$parser = xml_parser_create();

		xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
		xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);

		xml_parse_into_struct($parser,$this->rssData,$values,$tags);
		xml_parser_free($parser);

		// Loop through all the elements in the RSS XML file.  If an <item> tag
		// is found, it's children will be added to a array until the closing
		// tag is found. Then the array is added to a list of items
		// ($this->rssItems).
		for ($i=1; $i < count($values); $i++) {

			$tagName = "";
			$tagType = "";
			$tagValue = "";

			if (array_key_exists("tag", $values[$i])) {
				$tagName = $values[$i]["tag"];
			}
			if (array_key_exists("type", $values[$i])) {
				$tagType = $values[$i]["type"];
			}
			if (array_key_exists("value", $values[$i])) {
				$tagValue = $values[$i]["value"];
			}

			if ($values[$i]["tag"] == "item" && $values[$i]["type"] == "open") {
				// Looks like we found an <item> tag. Create a new array to
				// store it's children values as they will be found on the next
				// iteration of the loop.
				$rssItem = $this->newItem();
			}
			if ($values[$i]["tag"] == "item" && $values[$i]["type"] == "close" && isset($rssItem)) {
				// </item> tag closed. Store the read item information.
				$this->rssItems[] = $rssItem;
				unset($rssItem); // No item information will be saved when this doesn't exist.
			}

			if (array_key_exists($tagName, $this->saveItems) && isset($rssItem)) {
				// Found a tag that we want to store and that's part of an
				// <item>. Save it.
				switch($this->saveItems[$tagName]) {
					case "string":
						$rssItem[$tagName] = $tagValue;
						break;
					case "date":
						$rssItem[$tagName] = strtotime($tagValue);
						break;
					default:
						print("Don't know how to handle type ".$this->saveItems[$tagName].". Aborting.");
						exit(1);
						break;
				}
			}
		}

	}
}

$db = new RSSDB($db_hostname, $db_username, $db_password, $db_database);
$parser = new RSSParser($file);

$db->clearRssItems(); // Delete old RSS items.

foreach($parser->rssItems as $rssItem) {
	$db->saveRssItem(
		$rssItem["title"],
		$rssItem["link"],
		$rssItem["description"],
		$rssItem["dc:creator"],
		$rssItem["dc:date"],
		$rssItem["dc:subject"]
	);
}
I haven't done much with php in while so any help would be appreciated :D
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Help adding data to RSS feed field

Post by Benjamin »

Code: Select all

$text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
$pieces = explode(" ", $text);
$newText = implode(" ", array_merge(array_slice($pieces, 0, 70), array('<span class="full">'), array_slice($pieces, 71), array('</span>')));
scriptacular
Forum Newbie
Posts: 6
Joined: Wed May 20, 2009 6:46 pm

Re: Help adding data to RSS feed field

Post by scriptacular »

Thanks for the response Benjamin, in your example where should i insert the code so that it is added to each description field as it loops through the feed? I've tried a few ways but the changes aren't showing up in the database when i run the script and pull in the RSS feed.
Post Reply