RSS/XML feed parser

Here's some php:

PHP
function xml_parser($page,$container,$tags,$number,$cdata) {
  if (!$number) {$number=100;}
  $stories=0;
  $xml=file_get_contents($page);
  preg_match_all("/<$container>.+<\/$container>/sU",$xml, $items);
  $items=$items[0];
  $itemsArray=array();
   foreach ($items as $item) {
    for($i=0; $i<count($tags); $i++) {
    preg_match("/<$tags[$i](.+)(<\/$tags[$i]>)/sU", $item, $tag);
    $this[$i]=preg_replace("/<$tags[$i]>(.+)(<\/$tags[$i]>)/sU",'$1',$tag);
    $this[$i]=array_map('html_entity_decode', $this[$i]);
    }
     if (count($itemsArray)<$number) {array_push($itemsArray, $this);}
   }
  $theData="<dl>";
  foreach ($itemsArray as $item) {
  for($i=0; $i<count($tags); $i++) {
  $data[$i]=$item[$i][0];    }
   $title=$data[0];
   $dpatterns[0]="/<img(.+)><\/img>/sU"; $dreplacements[0]='<img$1>';
   $dpatterns[1]="/<img(.+)\/>/sU"; $dreplacements[1]='<img$1>';
   $dpatterns[2]="/<(\/|)content?(.+|)>/sU"; $dreplacements[2]='';
   $dpatterns[3]="/border=\"0\"/sU"; $dreplacements[3]='';
   if ($cdata!='hide') {
    $dpatterns[4]="/<\!\[CDATA\[(.+)\]\]>/sU"; $dreplacements[4]='$1';
   }
   else {
    $dpatterns[4]="/<\!\[CDATA\[(.+)\]\]>/sU"; $dreplacements[4]='';
   }
   $description=preg_replace($dpatterns,$dreplacements,$data[1]);
   $link=preg_replace("/<link.+href=\"(.+)\"(.+|)\/>/sU",'$1',$data[2]);
   $date=$data[3];
   $theData.="
   <dt><a href=\"$link\">$title</a></dt>
   <dd class=\"story\">$description</dd>
   <dd>Date: $date</dd>\r";
  }
$theData.="</dl>";
return $theData;
}

$container='item';
$tags=array('title','description','link','pubDate');
$bbc=xml_parser("http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml",$container,$tags,10,'');
$cnn=xml_parser("http://rss.cnn.com/rss/cnn_topstories.rss",$container,$tags,10,'');

$container='entry';
$tags=array('title','content','link','published');
$flickr=xml_parser("http://api.flickr.com/services/feeds/photos_public.gne",$container,$tags,10,'');

Here's some HTML with PHP

HTML/PHP
<h2>bbc</h2>
<?php echo $bbc; ?>
<h2>cnn</h2>
<?php echo $cnn; ?>
<h2>flickr</h2>
<?php echo $flickr; ?>

Here's what we get... (the lastest feeds from the BBC, CNN, and flickr).

bbc

Senior cabinet ministers expect the PM to outline a timetable on Friday for her successor to be chosen.
Date: Thu, 23 May 2019 21:01:14 GMT
Bob Higgins, who "held supreme power" over young footballers' careers, is convicted of 45 offences.
Date: Thu, 23 May 2019 17:40:20 GMT
The US is hoping to extradite the Wikileaks co-founder, who is currently in jail in the UK.
Date: Thu, 23 May 2019 20:51:25 GMT
A top Chinese diplomat tells the BBC there could be "substantial" repercussions if the UK bars Huawei.
Date: Thu, 23 May 2019 15:04:11 GMT
Court sees image of the moment a police shot comes through pub window, striking another man in the head.
Date: Thu, 23 May 2019 17:09:22 GMT
Richard Ashcroft says Mick Jagger and Keith Richards have relinquished their claim on the song.
Date: Thu, 23 May 2019 16:39:57 GMT
Thousands of EU citizens have been unable to vote in the European elections, a campaign group says.
Date: Thu, 23 May 2019 17:17:34 GMT
Mark Zuckerberg hits back at calls to break up Facebook, as it reveals it removed a record number of hateful posts.
Date: Thu, 23 May 2019 19:06:33 GMT
Cwm Taf health board's chief executive calls the experiences of some families "heartbreaking".
Date: Thu, 23 May 2019 17:25:54 GMT
The prime minister hails his party's commanding success at the polls as "a victory for India".
Date: Thu, 23 May 2019 17:11:43 GMT

cnn

When Donald Trump chose Rex Tillerson to be his secretary of state in December 2016, he praised the former head of ExxonMobil for his "tenacity, broad experience and deep understanding of geopolitics." In a tweet touting the pick, Trump called Tillerson "one of the truly great business leaders of the world."
Date: Thu, 23 May 2019 17:51:51 GMT
White House press secretary Sarah Sanders reacts to President Trump's tweet calling Rex Tillerson "ill prepared and ill equipped to be Secretary of State."
Date: Thu, 23 May 2019 17:08:32 GMT
Former Secretary of State Rex Tillerson told lawmakers that Russian President Vladimir Putin was more prepared than President Donald Trump for their meeting in Hamburg, Germany, putting US officials at a disadvantage, a Democratic House Foreign Affairs committee aide told CNN Wednesday.
Date: Thu, 23 May 2019 12:46:45 GMT
President Donald Trump leveraged his claims to be a world-class deal maker to win the presidency, touting that his fabled negotiating skills would reap benefits for America. The businessman who hired a writer to pen "The Art of the Deal" was supposed to translate his real estate prowess into governing. But the longer he stays in office, the better we understand why his ghostwriter now says the book was a sham.
Date: Thu, 23 May 2019 10:51:32 GMT
Former Secretary of State Rex Tillerson quietly met with the top Democrat and Republican on the House Foreign Affairs Committee Tuesday for an interview that focused primarily on his time in the Trump administration, a congressional aide with direct knowledge of the discussion confirmed to CNN.
Date: Wed, 22 May 2019 22:51:07 GMT
Republicans senators say President Donald Trump has agreed to a $19.1 billion disaster aid bill, providing relief to a country beset by devastating floods, wildfires and hurricanes without funding for the border that the President had sought, and that the Senate is expected to vote as soon as Thursday afternoon to advance the measure.
Date: Thu, 23 May 2019 19:33:31 GMT
President Donald Trump -- so far -- has been reluctant to give Nancy Pelosi any kind of nickname. Well, here are a few ideas.
Date: Thu, 23 May 2019 17:43:15 GMT
Radio host Howard Stern tells CNN's Anderson Cooper that he was shocked to receive a phone call from President Donald Trump when he was about to go on stage to be endorsed by Sarah Palin. See the full interview tonight on AC 360.
Date: Thu, 23 May 2019 20:25:51 GMT
Date:
White House counselor Kellyanne Conway claims that House Speaker Nancy Pelosi is not "pro-woman" because Pelosi declined to speak with Conway about negotiations with the White House.
Date: Thu, 23 May 2019 20:33:17 GMT

flickr

janj4365 posted a photo:

Date: 2019-05-23T00:44:55Z
Toaster - Black Decker 2-Slice Extra Wide Slot Toaster, Classic, Stainless Steel

Midori Chan Kawaii posted a photo:

Toaster - Black Decker 2-Slice Extra Wide Slot Toaster, Classic, Stainless Steel


☑️ Toaster - Black Decker 2-Slice Extra Wide Slot Toaster, Classic, Stainless Steel
☑️ Click here to See Details on Ebay
☑️ Click here to See Details on Amazon
☑️ This post contains an Amazon & Ebay affiliate link, we may earn a commission on your purchase at no additional cost to you.
☑️ Visit our website: sansinichi.blog

Date: 2019-05-23T00:45:05Z
TK4594 Mack RD Roll on Roll Off Garbage Truck

davieferdinand posted a photo:

TK4594 Mack RD Roll on Roll Off Garbage Truck

Date: 2019-05-23T00:45:08Z
Grain Silo Mallee district Victoria

laurie.g.w posted a photo:

Grain Silo Mallee district Victoria

Grain Silo Mallee district Victoria one of the many un-painted silos.

Date: 2019-05-23T00:55:35Z

Venessa Nina posted a photo:

Date: 2019-05-23T00:56:13Z
1558572201168014185

minhproooo1 posted a photo:

1558572201168014185

Date: 2019-05-23T00:45:01Z
P33A5316

duxlax posted a photo:

P33A5316

Date: 2019-05-23T00:55:25Z
20190512_040244

cynakin@att.net posted a photo:

20190512_040244

Date: 2019-05-23T00:55:50Z

198237@students.avhsd.org posted a photo:

Date: 2019-05-23T00:44:44Z
IMG_20190517_160018

KayOne73 posted a photo:

IMG_20190517_160018

Date: 2019-05-23T00:55:53Z

Comments

#1
2007-03-02 dumb_dave says :

Sorry, I'm new to this stuff, willing to learn and all that, but I don't get the idea. Copy that snippet of PHP code into a file and call it, say, parser.php. Copy the other snippet of HTML into a file and call it, for lack of inventiveness, parser.html. Right so far? If so, where's the intermediate step? How does this HTML "call" or "include" the PHP in order to function? Or am I missing something so basic that even asking this will earn me the cherished "Idiot of the Day Award"? Thanks.

#2
2007-03-02 BonRouge says :

dave,
You can include the php or just have it in one page. The page would have a '.php' extension - not '.html.'
Here's a simple example of this page (with no style or anthing) in one file.
Save it and change the extension to '.php'. If you don't have a server installed on your machine, you'll have to upload it to a remote server to view it.
If you want, you can take the php code out of that page and save it in a different file and include it into the page - that way, you could use it on more than one page if you wanted.

I hope that makes it a bit clearer.

#3
2007-03-02 dumb_dave says :

Thanks for the explanations. Much clearer now and ... yes, it indeed works like a champ. (Maybe I was just too tired? Putting 1 and 1 together and coming up with 11 instead of two?) Best regards and thanks for all the tips elsewhere as well.

#4
2007-03-07 dumb_dave says :

Useful indeed, BonRouge, but how does one display the <description> tagged material that is buried behind things like <![CDATA[ <p> etc.? Is the PHP code easily modified to handle that? And if so, can one apply it selectively? That is, show the fuller "description" material for one site but then reduce the next site entry to "headines" only (i.e., "titles" and "links") and then toggle the next one back to fuller details? Hope this is not a major headache, but it's beyond my ability to work it out at this stage ... and everything tried brought the larger process to a grinding halt. (This isn't a do-my-homework-for-me question. I'm bewildered by the code.) Thanks.

#5
2007-03-07 BonRouge says :

dave,
I thought I'd already sorted out the problem of data wrapped in the CDATA stuff. Does the code have a problem? If you could show me where it's not working, I'll try to improve it.
As for choosing whether to show that particular data or not, yes - I think you could do that by adding another variable. You see near the top where there's a preg_replace() to remove the CDATA tags? You could put that in an if statement - if the variable is not present, remove the CDATA tags, if it is, leave them where they are.
Does that make sense?

#6
2007-03-10 BonRouge says :

dave,
I think I found the problem and sorted it out. As you can see, it seems to work OK now. Some of the characters in the Lockergnome feed don't show right on this page though. I wonder if it's anything to do with me being in Japan. Do you see strange characters?

#7
2007-05-01 Ice says :

I have been trawling the web for days looking for something like this. Thanks a WHOLE lot man. I was also wondering if you can modify this parser to merge these fields and display, say, only the latest 10 items? wine

#8
2007-11-02 steve says :

thanks sorted out my cdata parasing problem, seems that is not too clear in the docs

s

#9
2019-04-28 Suezef says :

sildenafil without a prescription

#10
2019-04-28 Evazef says :

tadalafil

#11
2019-04-28 Terrellimary says :

? - . - , , ! - ! <b> </b>, 80% . , <b> </b>. , . . - <b> </b>. <b></b> <b> </b> <b></b>. <b> - </b> . <b></b> <b></b> <b> </b> <b> 1 </b>. ! ! -! <b> </b> --> http://c.trktp.ru/ud6b

#12
2019-04-28 SylviaTaimb says :



-----------------------------------
, , . - . interchange ( ) - , , , . - , . , . , . , , , . 0,75%-3%. . ?


, :

<a href="https://mastertarget.ru/scripts/click.php?a_aid=esif22&amp;a_bid=39b43ce0" target="_blank"><strong> </strong><br/></a><img style="border:0" src="https://mastertarget.ru/scripts/imp.php?a_aid=esif22&amp;a_bid=39b43ce0" width="1" height="1" alt="" />

#13
2019-04-28 Terrellimary says :

? - . - , , ! - ! <b> </b>, 80% . , <b> </b>. , . . - <b> </b>. <b></b> <b> </b> <b></b>. <b> - </b> . <b></b> <b></b> <b> </b> <b> 1 </b>. ! ! -! <b> </b> --> http://c.trktp.ru/ud6b

Comment form

Please type the word 'wife' here:

BB code available :

  • [b]...[/b] : bold
  • [it]...[/it] : italic
  • [q]...[/q] : quote
  • [c]...[/c] : code
  • [url=...]...[/url] : url