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

Campaigners say plans for a third runway should be cancelled, but the airport says it will appeal.
Date: Thu, 27 Feb 2020 15:25:39 GMT
The virus was passed on in Italy and Tenerife and brings the total number of UK cases to 15.
Date: Thu, 27 Feb 2020 16:30:03 GMT
Michael Gove says the UK will not accept any alignment with EU laws, as he announces the UK's strategy.
Date: Thu, 27 Feb 2020 14:09:38 GMT
Residents in two Midlands towns and people living in parts of East Yorkshire have been evacuated.
Date: Thu, 27 Feb 2020 15:57:23 GMT
Relatives of the Australian nine-year-old say they will give the crowdfunded cash to charity.
Date: Thu, 27 Feb 2020 16:06:26 GMT
Staff claim they often miss out on overtime amid allegations of poor cleaning practices at some branches.
Date: Thu, 27 Feb 2020 00:26:10 GMT
It is believed 12 arrests have been made, 15 stolen vehicles found and £90,000 seized.
Date: Thu, 27 Feb 2020 13:03:30 GMT
More drugs than ever before are reaching the UK, costing society billions each year, a report says.
Date: Thu, 27 Feb 2020 15:19:45 GMT
The Newsnight host is named network presenter of the year for quizzing the Duke of York.
Date: Thu, 27 Feb 2020 10:09:58 GMT
The house was left close to the cliff edge after a storm destroyed a large chunk of land.
Date: Thu, 27 Feb 2020 00:06:48 GMT

cnn

Date: Thu, 27 Feb 2020 13:51:43 GMT
Officials at the US Centers for Disease Control and Prevention have warned that the novel coronavirus will spread in US communities, and a case announced on Wednesday might be the country's first instance of it.
Date: Thu, 27 Feb 2020 14:47:18 GMT
The Dow and S&P 500 fell into correction territory on Thursday amid a continued selloff of US stocks. Investors continue to fret over the coronavirus outbreak.
Date: Thu, 27 Feb 2020 16:33:06 GMT
A patient being treated for novel coronavirus at UC Davis Medical Center was admitted to the facility last week but wasn't tested until Sunday, according to a letter sent to UC Davis staff and obtained by CNN.
Date: Thu, 27 Feb 2020 14:39:00 GMT
During a news conference to discuss the coronavirus threat to the US, President Donald Trump criticized Democratic leaders Nancy Pelosi and Chuck Schumer.
Date: Thu, 27 Feb 2020 00:39:17 GMT
As an infectious disease physician and medical microbiologist who has successfully treated patients with Ebola virus disease in the US, I am being asked a lot of questions about the new coronavirus, Covid-19.
Date: Wed, 26 Feb 2020 21:47:14 GMT
CNN's Dr. Sanjay Gupta fact-checks his exchange with President Donald Trump during a coronavirus press conference when Trump claimed the flu had higher death rates than the coronavirus.
Date: Thu, 27 Feb 2020 12:24:32 GMT
• Shooting turns complex into a 'war zone' • Molson Coors employee says talking to a co-worker before shooting likely saved his life
Date: Thu, 27 Feb 2020 12:12:41 GMT
• Opinion: For once, Michael Bloomberg had a good night • Klobuchar dishes on viral debate photo
Date: Thu, 27 Feb 2020 04:59:38 GMT
Very big waves are coming to the Great Lakes, with swells that could exceed 14 feet Thursday, according to CNN meteorologist Michael Guy.
Date: Thu, 27 Feb 2020 15:14:59 GMT

flickr

Chico Bello posted a photo:

Date: 2020-02-27T16:38:43Z

breancaofficial posted a photo:

Date: 2020-02-27T16:38:53Z
AN5I3004.jpg

Les Stills posted a photo:

AN5I3004.jpg

Date: 2020-02-27T16:39:07Z
2020-02-27_09-38-56

bamee9952 posted a photo:

2020-02-27_09-38-56

Date: 2020-02-27T16:39:09Z

kim.421 posted a photo:

Date: 2020-02-27T16:39:09Z

donovanlcsw posted a photo:

Date: 2020-02-27T16:38:47Z
Ferry de Puerto Natales à Puerto Montt

laverdant.alexis posted a photo:

Ferry de Puerto Natales à Puerto Montt

Date: 2020-02-27T16:38:54Z
red-billed hornbill

larslaguna posted a photo:

red-billed hornbill

Date: 2020-02-27T16:38:56Z
Schnellkäfer Selatosomus cruciatus, 11mm lang

naturgucker.de posted a photo:

Schnellkäfer Selatosomus cruciatus, 11mm lang

Kreuzbinden-Schnellkäfer (Selatosomus cruciatus)
(c) Jörg Siemers

Date: 2020-02-27T16:38:58Z
Condor des Andes ~ Vultur gryphus

faaolan posted a photo:

Condor des Andes ~ Vultur gryphus

Date: 2020-02-27T16:39:00Z

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

#14
2019-05-27 Evazef says :

cafergot online

#15
2019-05-27 Janezef says :

advair prescription

#16
2019-05-27 Write Essay For Me says :

homework help

#17 #18
2019-05-27 cheap insurance auto says :

car insurance quote compare car insurance rates

#19
2019-05-27 Kimzef says :

phenergan 25 mg

#20
2019-05-27 RogovaEvridika1989Sig says :

fdf7 forum be is online


http://ph-90e13-3-1.com/

comprare is 5mg

#21
2019-05-27 Evazef says :

acytoclovir 800

#22
2019-05-27 Kiazef says :

sildenafil citrate over the counter

#23
2019-05-27 tjgjInill says :

virgin online casino free casino slot games vegas slots online <a href="http://redcasinoonline.com/">online - games</a>

#24
2019-05-27 Evazef says :

dapoxetine buy

#25
2019-05-28 losElome says :

slotomania on facebook slotomania free online slots game <a href="http://playersonlinecasinogo.com/">hollywood casino dit is spam slots online</a>

#26
2019-05-28 car insurance quotes says :

cheap car insurance online

#27
2019-05-28 aarp insurance auto says :

automobile insurance auto insurance quotes

#28
2019-05-28 Evazef says :

furosemide 40 mg

#29
2019-05-28 Annazef says :

tadalafil levitra 10 mg price

#30
2019-05-28 Kimzef says :

cafergot buy

#31
2019-05-28 Evazef says :

vardenafil 20mg tab

#32
2019-05-28 Kimzef says :

allopurinol 300 mg

#33
2019-05-28 Erotic Chat says :

cam girls web cam girls

#34
2019-05-28 Evazef says :

amoxicillin price

#35
2019-05-28 Janezef says :

vardenafil

#36
2019-05-28 Good Homework Music says :

boston college application essay custom writing

#37
2019-05-28 jhvReart says :

zone online casino games casino blackjack <a href="http://mylistonlinecasino.com/">winstar world casino</a>

#38
2019-05-28 Janezef says :

tadalafil generic

#39
2019-11-27 amibcobixc says :

#40
2019-11-27 xtgbbghnwy says :

#41
2019-11-27 iuajxcuvky says :

#42
2019-11-27 icdvlpofwm says :

#43
2019-11-27 wmledhrcen says :

<a href="http://genericlevitr.com/">generic levitra overnight delivery</a>

#44
2019-11-27 uygtbzhwut says :

#45
2019-11-27 qeodhabmlf says :

<a href="http://genericlevitr.com/">generic levitra overnight delivery</a>

#46
2019-11-27 ddmobpnspe says :

#47
2019-11-27 gliydpwwph says :

#48
2019-11-27 ezrrqmxauc says :

#49
2019-11-27 udfsqczvwp says :

#50
2019-11-27 hdljdehziu says :

#51
2019-11-27 MichaelSpini says :

http://www.paroxetine.team

#52
2019-11-27 sfssdexces says :

#53
2019-11-27 djqahoxvym says :

#54
2019-11-27 Kennethdaply says :

www.ventolin.us.com
generic atenolol

#55
2019-11-27 zyxhhytzsl says :

<a href="http://genericlevitr.com/">what is generic levitra</a>

#56
2019-11-27 chfyeqweli says :

#57
2019-11-27 lsseiiwaln says :

#58
2019-11-27 bbrtuhwgmj says :

#59
2019-11-27 gnfgtiocsz says :

#60
2019-11-27 kaqgntlnxb says :

#61
2019-11-27 wkzbcmirvk says :

#62
2019-11-27 sqaomydwnu says :

#63
2019-11-27 wpaqamzwnf says :

#64
2019-11-27 npqgpovhno says :

#65
2019-11-27 gcofvwowzu says :

<a href="http://genericlevitr.com/">generic levitra prices</a>

#66
2019-11-27 tnecjddrxe says :

<a href="http://genericlevitr.com/">adc levitra generic</a>

#67
2019-11-27 qnxxlrehxo says :

#68
2019-11-27 iiwvitelbc says :

#69
2019-11-27 hzobzroilm says :

#70
2019-11-27 ndpqofrxwk says :

#71
2019-11-27 MichaelSpini says :

http://zofran.us.com

#72
2019-11-27 kgdhurnnfz says :

#73
2019-11-27 ebqcqjdzdn says :

#74
2019-11-27 ffchomfrdu says :

#75
2019-11-28 bjyiqnrkiz says :

#76
2019-11-28 rijzeuitrt says :

#77
2019-11-28 Kennethdaply says :

www.buyprednisone.us.com
cheap methotrexate

#78
2019-11-28 lsshdhkvlm says :

<a href="http://genericlevitr.com/">buy cheap generic levitra</a>

#79
2019-11-28 vxtshfcjej says :

#80
2019-11-28 nfkwexgdiy says :

#81
2019-11-28 hxyqntwghr says :

#82
2019-11-28 pgozhesbbc says :

#83
2019-11-28 sxpyuwexbu says :

#84
2019-11-28 nfetnqpgrq says :

<a href="http://genericlevitr.com/">levitra generic pics</a>

#85
2019-11-28 mytlitgbhv says :

#86
2019-11-28 zcryadkncs says :

#87
2019-11-28 kdrgiqnnwr says :

<a href="http://genericlevitr.com/">generic levitra us</a>

#88
2019-11-28 eylqzwmatg says :

#89
2019-11-28 gsqxzqphwl says :

#90
2019-11-28 abqptczkjw says :

#91
2019-11-28 MichaelSpini says :

www.biaxin.company

#92
2019-11-28 gcqhabzupy says :

#93
2019-11-28 zfcolztesn says :

#94
2019-11-28 yzcestvncc says :

#95
2019-11-28 MichaelSpini says :

cheap trazodone
cheap stromectol

#96
2019-11-28 pupyidjxvo says :

#97
2019-11-28 pgtmsjsnvs says :

<a href="http://genericlevitr.com/">cheapest generic levitra online</a>

#98
2019-11-28 crhlqntouu says :

#99
2019-11-28 fdcdlhktgp says :

#100
2019-11-28 fecyudcqjn says :

#101
2019-11-28 asdmhiwxki says :

#102
2019-11-28 fpmtdiszlx says :

#103
2019-11-28 pbkfgjhcjt says :

#104
2019-11-28 avjlhbauid says :

#105
2019-11-28 bhjzsdedlj says :

#106
2019-11-28 jsvxfzifaw says :

#107
2019-11-28 cinrsikdvx says :

#108
2019-11-28 gdrkpoqbdk says :

#109
2019-11-28 vfrsqrmdva says :

#110
2019-12-27 MichaelSpini says :

lexapro generic cheap xenical

#111
2019-12-27 tgnpwwtwwr says :

Gabby dissection reach that the РІ 10 <a href="http://sildenafiltotake.com/">sildenafil 20mg</a>

#112
2019-12-27 MichaelSpini says :

cheap tadalafil valtrex

#113
2019-12-27 mjjouactpz says :

Twenty of zoonosis you bought a <a href="http://aaedpills.com/">ed meds pills drugs</a>

#114
2019-12-28 dmttbhpptt says :

and the Provocative-type PokР“mon coupled Sequencing Pipes Authenticity has a vagal to sixty on intraocular <a href="http://levitrars.com/">erectile medicines levitra</a>

#115
2019-12-28 Stewartmooma says :

buy tadalafil uk

#116
2019-12-28 ylxxsifkwm says :

Chuckles is most when a author of sly come to an end <a href="http://sildenafilfas.com/">sildenafil online</a>

#117
2019-12-28 dnugklwyaw says :

I opposite these this prepackaged (as a replacement for the most portion)

#118
2020-01-27 Janezef says :

prednisone 40 mg tablet

#119
2020-01-27 Kiazef says :

buy generic valtrex online cheap

#120
2020-01-28 dvgtThync says :

Danazol Yasmin Exelon <a href="http://diclofenacvoltarenax.com/#">Dostinex</a> Clomid

#121
2020-01-28 vdbrEmpow says :

cash till payday loan cash till payday loan direct payday loan lenders <a href="http://paydayloanjyd.com/#">a payday loan</a> payday loan no fax

#122
2020-02-27 Wimzef says :

hydrochlorothiazide 25

Comment form

Please type the word '' here:

BB code available :

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