1
votes

I have a local intranet site I am developing on which I want to display some rss feeds from other sites. Currently is is built on the Concrete5 CMS and I am using an RSS displayer plugin to display the feeds. The plugin uses SimplePie to parse the feed. By default, the plugin displays the entire RSS content. I've tweaked the plugin (SimplePie) to display only a title with link, date, and the first image in each post/entry. I found this function that I pass $item->get_content() to in order to get the first image's source:

function getFirstImage($text) {
    $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
$pattern = "/<img[^>]+\>/i";
preg_match($pattern, $text, $matches);
    $text = $matches[0];
return $text;
}
function scrapeImage($text) {

    $pattern = '/src=[\'"]?([^\'" >]+)[\'" >]/'; 
    preg_match($pattern, $text, $link);
$link = $link[1];
$link = urldecode($link);
return $link;
}

It works fine, the problem is that some of the feeds have ads in them which are sometimes placed before the actual post content, therefore this function returns the url of an ad. Obviously these RSS ads are targeted at people who use rss readers, but for displaying them on a site, they are very annoying.

If I try to target exact tags besides <img> within preg_match() I feel it will only work for the specific feed that I've taken the tag from. (For example, if I try to use preg_match() to find only images inside <p> tags)

How can I get the first image from the actual post that isn't an ad without having to change the code for each feed I want to display?

1
You might be violating the terms of the web site that produces the feed by removing the ads. - Shane Wealti
It is an intranet site, therefore it is not being displayed to the public. It is somewhat like a custom start page such as My Yahoo or iGoogle. I don't recall seeing ads when adding feeds to either of those sites, but then again I haven't used either in a while. Besides, there seems to be a big gray area there. On one hand why should they be able to monetize my site and my own content (if it were public)? on the other hand, I'm using their content and if I monetize my site then I'm monetizing their content. - biggles
It is an intranet site, therefore it is not being displayed to the public. — that just makes it less likely that you will be caught, it doesn't make it right or legal. - Quentin
It is somewhat like a custom start page such as My Yahoo or iGoogle. I don't recall seeing ads when adding feeds to either of those sites — Did you add the feed you are planning to use? Did My Yahoo! or iGoogle show the full content and not just headlines with links to the original article? - Quentin
On one hand why should they be able to monetize my site and my own content (if it were public)? — Which "they"? Mysterious source of content you are using? They aren't, so that's irrelevant. Google and Yahoo!? In search results? They are taking snippits and linking to you so you benefit, they also respect robots.txt. In RSS feed reading web applications? There is nothing stopping you putting adverts in your content. - Quentin

1 Answers

2
votes

I'm not sure if this would work for your situation but usually ad images come from a different domain or sub-domain than the regular content. You could try to filter out images based on the domain or sub-domain in the URL being different then the domain or sub-domain of the rss feed.