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

Boris Johnson's blueprint prompts attacks from Labour, the SNP and Lib Dems.
Date: Thu, 05 Dec 2019 16:13:42 GMT
BBC presenter tells Boris Johnson it is "not too late" for the PM to face questions from him.
Date: Thu, 05 Dec 2019 19:41:12 GMT
"Our democracy is what is at stake," the House Speaker says, as Mr Trump demands they act "fast".
Date: Thu, 05 Dec 2019 17:00:44 GMT
Global estimates suggest 142,000 people - mostly children - died from the disease in 2018
Date: Thu, 05 Dec 2019 18:20:05 GMT
Debts including credit card debt and personal loans have risen 11% to £119bn in two years.
Date: Thu, 05 Dec 2019 15:00:28 GMT
British heavyweight Anthony Joshua fields questions about Saudi 'sportwashing' human rights abuse claims with his re-match against Andy Ruiz Jr.
Date: Thu, 05 Dec 2019 18:46:17 GMT
The Brexit Party leader claims his party are "tearing chunks out of the Labour vote", as three of its MEPs quit.
Date: Thu, 05 Dec 2019 19:00:55 GMT
Transport and schools are severely affected by the nationwide walkout over pension changes.
Date: Thu, 05 Dec 2019 17:49:36 GMT
A third of the first Thomas Cook customers to claim refunds will not be paid within the 60-day target.
Date: Thu, 05 Dec 2019 19:29:28 GMT
They allegedly stole billions worldwide and conducted work for a Russian intelligence agency.
Date: Thu, 05 Dec 2019 18:41:47 GMT

cnn

House Speaker Nancy Pelosi issued a scathing warning Thursday to a reporter who asked her a question about her feelings about President Donald Trump: "Don't mess with me."
Date: Thu, 05 Dec 2019 17:22:11 GMT
Date:
House Speaker Nancy Pelosi (D-CA) delivered a powerful response after reporter asked whether or not she is moving forward with impeachment because she "hates" President Trump, as some Republicans have suggested.
Date: Thu, 05 Dec 2019 17:34:28 GMT
Don't miss what happened in Washington on Thursday morning: House Speaker Nancy Pelosi virtually guaranteed that President Donald Trump will be only the third president in American history to be impeached by the House.
Date: Thu, 05 Dec 2019 16:50:21 GMT
House Minority Leader Rep. Kevin McCarthy defends President Donald Trump after House Speaker Nancy Pelosi asks House Democrats to draft articles of impeachment.
Date: Thu, 05 Dec 2019 17:37:40 GMT
With his impeachment now all-but-certain, President Donald Trump and his allies are beginning to look past the House of Representatives, shifting their sights to the Republican-led Senate where the President will face trial.
Date: Thu, 05 Dec 2019 18:32:24 GMT
During the impeachment inquiry hearing, Rep. Matt Gaetz (R-FL) staunchly criticized Stanford Law School Professor Pamela S. Karlan's comment involving the President's son, Barron Trump.
Date: Thu, 05 Dec 2019 14:32:30 GMT
• READ: Trump appeal to Supreme Court to block House subpoenas • Analysis: Rulings against Trump on his tax returns may be tough to reverse
Date: Thu, 05 Dec 2019 18:54:46 GMT
• Clinton says Trump was so shocked he could barely talk when she conceded
Date: Thu, 05 Dec 2019 17:21:07 GMT
A 23-year-old woman in the Indian state of Uttar Pradesh was set on fire Thursday as she traveled to testify at the trial of two men accused of raping her, according to police.
Date: Thu, 05 Dec 2019 17:53:01 GMT

flickr

Out walking the dog. ❄️ #winterwonderland

Rundtomrundt posted a photo:

Out walking the dog. ❄️ #winterwonderland

View on Instagram ift.tt/2DRumiF

Date: 2019-12-05T19:43:14Z
P1140301.jpg

nicoleduranceau posted a photo:

P1140301.jpg

Date: 2019-12-05T19:43:20Z
Coloquio Internacional de Cultura Científica: “El periodista en la ciencia: experiencia y vocación”

FIL Guadalajara posted a photo:

Coloquio Internacional de Cultura Científica: “El periodista en la ciencia: experiencia y vocación”

Coloquio Internacional de Cultura Científica: “El periodista en la ciencia: experiencia y vocación” con Iván Carrillo y Andrea Obaid, en la Feria Internacional del libro en Guadalajara, México, jueves 5 de diciembre del 2019. (© FIL/PAULA ISLAS)

Date: 2019-12-05T19:43:31Z
PhotoSession 12 2019

rossbrodskiy.photo posted a photo:

PhotoSession 12 2019

Date: 2019-12-05T19:43:40Z
IMG_20191205_182626

mattiascavero posted a photo:

IMG_20191205_182626

dav

Date: 2019-12-05T19:43:40Z
😊

vd0143462 posted a photo:

😊

Date: 2019-12-05T19:43:43Z
Naples, Torre Del Greco

bubifraci posted a photo:

Naples, Torre Del Greco

rhdr

Date: 2019-12-05T19:43:44Z
Discord_QoIR3dEAKd

olimpos907 posted a photo:

Discord_QoIR3dEAKd

Date: 2019-12-05T19:43:16Z

jenniferbealsgroup posted a photo:

Date: 2019-12-05T19:43:17Z

4stolls posted a photo:

Date: 2019-12-05T19:43:25Z

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 :

Comment form

Please type the word 'CSS' here:

BB code available :

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