Jump to content

RSS Filter


opy01

Recommended Posts

Hello, I am just learning PHP and making a personal web site for myself.  This is not my first site and I am hosting it from my house.  I am good at html and decent at manipulating different code for my own use but not too good at making up my own.  I have saltwater aquariums with 1 controller that outputs all the sensor readings etc to an rss file. 

 

I am using rss2html from feedforall to display the data that I want to a page on my site.  Now I want to devide up the data between tanks to two different areas.  For this I amusing this site http://76.79.252.38/index.php to filter then feeding the results through rss2html with great success.  I dont want to use someone elses site, I want to host evewrything myself.  I want a simple PHP script that will filter my rss.xml file for one word and then output the feed to rss2html.

 

This is what I currently do to get my data with the word BC29 in the title.  The title has multiple words but I just want to search fo a word

<?php 
$XMLFILE = "http://76.79.252.38/index.php?URL=http://mysite.com/rss/rss.xml&KEYWORDS=BC29&c1=&c2=checked&c3="; 
$TEMPLATE = "http://mysite.com/rss.html"; 
include("rss2html.php"); 
?>

 

I found a code that does something similar but I cant get it to work.

<?php

$xml =  simplexml_load_file($_GET['input']);

$ix = 0;
while ($ix < count($xml->channel->item) ) {
  if (!array_in_array($xml->channel->item[$ix]->category,$_GET['categories'])) {
    unset($xml->channel->item[$ix]);
  } else {
    $ix++;
  }
} 

echo $xml->asXML();

// in_array that accepts two arrays (and returns true if it finds any common value)
function array_in_array($a, $b) {
  if (!is_array($a))
    return in_array($a, $b);

  foreach ($a as $elem) {
    if (in_array($elem, $b))
      return true;
  }
  return false;
}

?>

 

and the path rss2html would point to for the feed would be this with my server, my feed and the keyword:

 

http://yourserver.com/rssfilter/index.php?input=http%3A%2F%2Fvg.no%2Ffeed&categories%5B%5D=Sport&categories%5B%5D=Rampelys

 

This was set up for a news feed so it looks to the catagory but all I want from this is to search the title for one keyword and to output it to its own feed like the above site I am currently using so that rss2html can put the wanted data where it needs to be.  I assume I need to change the words "category" to "title" but the second half is setup for a dual word search and the original writer stated that it only returns the results of one of those words anyways.

 

Keep in mind I am new to PHP so if you want to explain stuff thats fine, but please dont laugh at me lol.  Any help is much appreciated.  Thanks!

Link to comment
https://forums.phpfreaks.com/topic/201709-rss-filter/
Share on other sites

Ok I may have an easier solution but I am having a little trouble getting the desired effect.  I found a kind of messy code and cleaned it up a little.  When I finally got it to run without errors all it returns is the word "array".  I know it is reading the rss because I can debug and it shows all the content from one line of the rss.  If I use the print_r command it shows the last line of the rss file so it's not filtering the keyword.

 

I think my first goal is to actually get it to show the contents of the title and description without the "title" and "description" labels.  Then I can focus on getting the filtered content.  Any help is much appreciated.

 

<?php
$keywords = "BC29";

function has_keywords($rss, $keywords) 
{ 
  $found = false; 
  { 
    if (stripos($rss, $keywords) !== 0) 
    { 
      $found = true; 
    } 
  } 
  return $found; 
} 

$rss = simplexml_load_file("http://opruitt.serveftp.net:8383/rss/rss.xml"); 
foreach ($rss->channel->item as $i) 
{ 
  if ( 
      has_keywords($i->title, $keywords) 
  ) 
  { 
    $news = array 
    ( 
        "title" => $i->title, 
        "description" => $i->description 
    ); 
  } 
} 
print_r ($news); 
?> 

Link to comment
https://forums.phpfreaks.com/topic/201709-rss-filter/#findComment-1058326
Share on other sites

Ok I am a little further.  I can view the last entry of my rss feed.  I even got it to display the way I want with the help of some extra code.  I still cant figure out how to filter properly.  The command "echo $news["title"]["description"]" worked at one point but I have been changing stuff to try to figure out why it wont filter or show all posts.  I just tried to get it back to the way it was with no luck.  Now when ["description"] is in the command it all goes away.  If I just use "echo $news["title"]" the title shows up but as soon as I add ["description"] it goes away.  It was working earlier but now its not for some reason.  I guess thats not important since it didnt display the way I wanted.

 

This is my new script.  I know its needs the $keywords since I removed the BC29 and it gave me an error it just doesnt return the posts with that keyword in it.  In fact it is still only showing just the last post in the rss.  This is driving me so nuts I am pulling my hair out.  Can anyone see any reason this isnt showing all of the posts or even one with that keyword? :confused:

 

<?php 
$keywords = "BC29"; 

function has_keywords($rss, $keywords) 
{ 
  $found = false; 
  { 
    if (stripos($rss, $keywords) !== 0) 
    { 
      $found = true; 
    } 
  } 
  return $found; 
} 

$rss = simplexml_load_file("http://opruitt.serveftp.net:8383/rss/rss.xml"); 
foreach ($rss->channel->item as $i) 
{ 
  if ( 
      has_keywords($i->title, $keywords) 
  ) 
  { 
    $news = array 
    ( 
        "title" => $i->title, 
        "description" => $i->description 
    ); 
  } 
} 
function displayArrayContentFunction($arrayname,$tab="&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp",$indent=0) {
$curtab ="";
$returnvalues = "";
while(list($key, $value) = each($arrayname)) {
  for($i=0; $i<$indent; $i++) {
   $curtab .= $tab;
   }
  if (is_array($value)) {
   $returnvalues .= "$curtab$key : Array: <br />$curtab{<br />\n";
   $returnvalues .= displayArrayContentFunction($value,$tab,$indent+1)."$curtab}<br />\n";
   }
  else $returnvalues .= "$value<br />\n";
  $curtab = NULL;
  }
return $returnvalues;
} 
echo displayArrayContentFunction($news); 
?> 

Link to comment
https://forums.phpfreaks.com/topic/201709-rss-filter/#findComment-1058515
Share on other sites

well after almost 24 hours straight I think I almost have it.  I used the code in the first post and changed categories to guid since the guids never change in my rss.  I want guids 1-4 in one area and guids 5-8 in another.  I can actually choose what guids I want where so it works out perfect!  I now dont even need rss2html.  I am using this as my filter:

 

<?php
$xml =  simplexml_load_file("http://opruitt.serveftp.net:8383/rss/rss.xml");

$ix = 0;
while ($ix < count($xml->channel->item) ) {
  if (!array_in_array($xml->channel->item[$ix]->guid,$_GET['guid'])) {
    unset($xml->channel->item[$ix]);
  } else {
    $ix++;
  }
} 

echo $xml->asXML();

// in_array that accepts two arrays (and returns true if it finds any common value)
function array_in_array($a, $b) {
  if (!is_array($a))
    return in_array($a, $b);

  foreach ($a as $elem) {
    if (in_array($elem, $b))
      return true;
  }
  return false;
}
?>

 

and I use this to pull the data into the page:

 

<?php 
$rss = simplexml_load_file("http://opruitt.serveftp.net/rss.php?guid%5B%5D=1&guid%5B%5D=2&guid%5B%5D=3&guid%5B%5D=4"); 
foreach ($rss->channel->item as $i) 
{ 
{ 
echo "<p>" . $i->title . "</p>"; 
echo "<p>" . $i->description . "</p>"; 
} 
} 
?> 

 

I will clean it up some as I go but I think it will clean up my server alot without all those files lol.

Link to comment
https://forums.phpfreaks.com/topic/201709-rss-filter/#findComment-1058623
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.