rssreader

Gesponsorde koppelingen

PHP script bestanden

  1. rssreader

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php

/*
 * Rss reader/parser
 * @author Ruud Verbij, [email protected]
 * @version 1.0
 * @usage at the bottom
 */


class rssReader {
    private $url;
    private $source;
    private $titel;
    private $eersteItem;
    private $aantalItems;
    private $extraOptiesArray;

    private $titels;
    private $inhouds;
    private $links;
    private $pubDates;
    private $categorys;
    private $extraOpties;

    /*
     * Constructor
     * @param url = url to rss file
     * @require url != null
     */

    function __construct($url) {
        // setting private vars
        $this->url = $url;
        $this->source = file_get_contents($url);

        // search rss-feed title
        $beginTitel = strpos($this->source,"<title>")+7;
        $eindTitel  = strpos($this->source,"</title>",$beginTitel);
        $this->titel = substr($this->source,$beginTitel,$eindTitel-$beginTitel);

        // finding first item
        $this->eersteItem = strpos($this->source,"<item>",0) + 6;

        // finding number of items in the rss-file
        $this->aantalItems = substr_count($this->source,"<item>");
    }


    /*
     * @return index of first Item
     */

    function getFirstItem() {
        return $this->eersteItem;
    }


    private function getSource() {
        return $this->source;
    }


    /*
     * Sets extra search options to parse
     * @param $extraSearchOptions -> array with Integer index
     */

    function setExtraSearchOptions($extraSearchOptions) {
        $this->extraOptiesArray = $extraSearchOptions;
    }


    /*
     * Parsing the rss-file
     * @param titel,link,descr,pubDate,category booleans
     * searching for if boolean is set true
     * @require all true values are present in each item in the rss-feed
     */

    function parse($title = false,$link = false,$descr = false,$pubDate = false,$category = false,$extra = false) {
        $beginZoektocht = $this->getFirstItem() - 6;

        // for each item
        for($i = 0; $i < $this->aantalItems; $i++) {
            // localising item
            $beginZoektocht = strpos($this->getSource(),"<item>",$beginZoektocht);
            
            // check what should be called
            if($title)
                $this->titelZoeken($beginZoektocht);
            if($link)
                $this->linkZoeken($beginZoektocht);
            if($descr)
                $this->descrZoeken($beginZoektocht);
            if($pubDate)
                $this->pubDateZoeken($beginZoektocht);
            if($category)
                $this->categoryZoeken($beginZoektocht);
            if($extra)
                $this->extraZoeken($beginZoektocht);

            $beginZoektocht = strpos($this->getSource(),"</item>",$beginZoektocht+1);
        }  
    }


    private function extraZoeken($begin) {
        for($i=0;$i<count($this->extraOptiesArray);$i++) {
            $tekst = $this->extraOptiesArray[$i];
            $beginZoektocht = strpos($this->getSource(),"<".$tekst.">",$begin)+strlen($tekst)+2;
            $eindZoektocht  = strpos($this->getSource(),"</".$tekst.">",$beginZoektocht);
            $this->extraOpties[$tekst][] = substr($this->getSource(),$beginZoektocht,$eindZoektocht-$beginZoektocht);
        }
    }



    private function categoryZoeken($begin) {
        $beginZoektocht = strpos($this->getSource(),"<category>",$begin) + 10;
        $eindZoektocht  = strpos($this->getSource(),"</category>",$beginZoektocht);
        $this->categorys[] = substr($this->getSource(),$beginZoektocht,$eindZoektocht-$beginZoektocht);
        return $eindZoektocht;
    }


    private function pubDateZoeken($begin) {
        $beginZoektocht = strpos($this->getSource(),"<pubDate>",$begin) + 9;
        $eindZoektocht  = strpos($this->getSource(),"</pubDate>",$beginZoektocht);
        $this->pubDates[] = substr($this->getSource(),$beginZoektocht,$eindZoektocht-$beginZoektocht);
        return $eindZoektocht;
    }


    private function descrZoeken($begin) {
        $beginZoektocht = strpos($this->getSource(),"<description>",$begin) + 13;
        $eindZoektocht  = strpos($this->getSource(),"</description>",$beginZoektocht);
        $this->inhouds[] = substr($this->getSource(),$beginZoektocht,$eindZoektocht-$beginZoektocht);
        return $eindZoektocht;
    }


    private function titelZoeken($begin) {
        $beginZoektocht = strpos($this->getSource(),"<title>",$begin) + 7;
        $eindZoektocht  = strpos($this->getSource(),"</title>",$beginZoektocht);
        $this->titels[] = substr($this->getSource(),$beginZoektocht,$eindZoektocht-$beginZoektocht);
        return $eindZoektocht;
    }


    private function linkZoeken($begin) {
        $beginZoektocht = strpos($this->getSource(),"<link>",$begin) + 6;
        $eindZoektocht  = strpos($this->getSource(),"</link>",$beginZoektocht);
        $this->links[] = substr($this->getSource(),$beginZoektocht,$eindZoektocht-$beginZoektocht);
        return $eindZoektocht;
    }


    /*
     * set number of items to process
     * does not require $aantal <= getCountItems(), will be checked
     */

    function setCountItems($aantal) {
        if($aantal <= $this->getCountItems())
            $this->aantalItems = $aantal;
    }


    /*
     * @return number of items processed
     */

    function getCountItems() {
        return $this->aantalItems;
    }


    /*
     * @return rss-feeds title
     */

    function getRssTitle() {
        return $this->titel;
    }


    /*
     * @return items extra[$stringKey] of 'which'-item
     * if $which >= $this->getCountItems() -> $which = $this->getCountItems()-1
     * if $stringKey hasnt been parsed; returns false
     */

    function getExtra($which,$stringKey) {
        if(in_array($stringKey,$this->extraOptiesArray)) {
            if($which < $this->getCountItems()) {
                return $this->extraOpties[$stringKey][$which];
            }

            else
                return $this->extraOpties[$stringKey][$this->getCountItems() - 1];
        }

        else
           return false;
    }


    /*
     * @return items title of 'which'-item
     * if $which >= $this->getCountItems() -> $which = $this->getCountItems()-1
     */

    function getTitle($which) {
        if($which < $this->getCountItems())
            return $this->titels[$which];
        else
            return $this->titels[$this->getCountItems() -1];
    }


    /*
     * @return items description of 'which'-item
     * if $which >= $this->getCountItems() -> $which = $this->getCountItems()-1
     */

    function getDescription($which) {
        if($which < $this->getCountItems())
            return $this->inhouds[$which];
        else
            return $this->inhouds[$this->getCountItems() -1];
    }


    /*
     * @return items link of 'which'-item
     * if $which >= $this->getCountItems() -> $which = $this->getCountItems()-1
     */

    function getLink($which) {
        if($which < $this->getCountItems())
            return $this->links[$which];
        else
            return $this->links[$this->getCountItems() -1];
    }


    /*
     * @return items pubDate of 'which'-item
     * if $which >= $this->getCountItems() -> $which = $this->getCountItems()-1
     */

    function getPubDate($which) {
        if($which < $this->getCountItems())
            return $this->pubDates[$which];
        else
            return $this->pubDates[$this->getCountItems() -1];
    }


    /*
     * @return items category of 'which'-item
     * if $which >= $this->getCountItems() -> $which = $this->getCountItems()-1
     */

    function getCategory($which) {
        if($which < $this->getCountItems())
            return $this->categorys[$which];
        else
            return $this->categorys[$this->getCountItems() -1];
    }


    /*
     * @return item which as array with keys title,description,link,pubDate and category, if set true
     * if $which >= $this->getCountItems() -> $which = $this->getCountItems()-1
     */

    function getItem($which,$title = false,$description = false,$link = false,$pubDate = false,$category = false,$extra=false) {
        $which = ($which > $this->getCountItems()) ? $this->getCountItems()-1 : $which;
        $result[] = "";

        if($title)
            $result['title'] = $this->getTitle($which);

        if($description)
            $result['description'] = $this->getDescription($which);

        if($link)
            $result['link'] = $this->getLink($which);

        if($pubDate)
            $result['pubDate'] = $this->getPubDate($which);

        if($category)
            $result['category'] = $this->getCategory($which);

        if($extra)
            for($i=0;$i<count($this->extraSearchOptions);$i++)
                $result[$extraSearchOptions[$i]] = $this->getExtra($which,$extraSearchOptions[$i]);

        return $result;
    }


    /*
     * html_entity_decode for all descriptions
     * no return
     */

    function decodeDescription() {
        for($i = 0; $i < $this->getCountItems(); $i++)
            $this->inhouds[$i] = html_entity_decode($this->getDescription($i));
    }


    /*
     * strips descriptions (removes <CDATA and ]]>), specially for nu.nl
     */

    function stripDescription($tekst = "") {
        if(empty($tekst)) {
            for($i = 0; $i < $this->getCountItems(); $i++) {
                $beginInhoud  = strpos($this->getDescription($i),"CDATA")+6;
                $eindigInhoud = strpos($this->getDescription($i),"]]>");
                $this->inhouds[$i] = substr($this->getDescription($i),$beginInhoud,$eindigInhoud-$beginInhoud);
            }
        }
else {
            $beginInhoud  = strpos($tekst,"CDATA")+6;
            $eindigInhoud = strpos($tekst,"]]>");
            return substr($tekst,$beginInhoud,$eindigInhoud-$beginInhoud);
        }
    }


    /*
     * limits $what at length $length
     * if($wrap), words are finished if needed untill a whitespace has been reached
     * integer $dots says how many dots have to be add if new.length < old.length
     * returns new string
     */

    static function limit($what,$length,$wrap = false,$dots = 0) {
        $lengteNu = strlen($what);
        if(($lengteNu > $length) && $wrap) {
            $length = strpos($what," ",$length);
            if($length <= 0)
                       $length = $lengteNu;
        }

        $what = substr($what,0,$length);
        if($lengteNu > $length)
            $what .= str_repeat(".",$dots);
        return $what;
    }
}

?>

<-- ************************************************ -->
<?php
/* usage1: setting extra search options yourself */
$rss = new rssReader('http://www.nu.nl/deeplink_rss2/index.jsp?r=Algemeen');
$extraOptions = array('title','description','link');
$rss->setExtraSearchOptions($extraOptions);
$rss->parse(false,false,false,false,false,true);
$rss->setCountItems(4);

echo $rss->getRssTitle() . "<br>";
for($i=0;$i<$rss->getCountItems();$i++) {
    echo "<a href=\"".$rss->getExtra($i,'link')."\"><b>".$rss->getExtra($i,'title')."</b></a><br>";
    echo $rss->limit($rss->stripDescription($rss->getExtra($i,'description')),100,true,3) . "<br>";
}

?>

<hr>
Will do the same as
<hr>
<?php
/* usage2: using buildin search options */
$rss = new rssReader('http://www.nu.nl/deeplink_rss2/index.jsp?r=Algemeen');
$rss->parse(true, true, true);
$rss->decodeDescription();
$rss->stripDescription();
$rss->setCountItems(4);

echo $rss->getRssTitle() . "<br>";
for($i=0;$i<$rss->getCountItems();$i++) {
    $items[] = $rss->getItem($i,true,true,true);
    echo "<a href=\"$items[$i]['link']\"><b>".$items[$i]['title']."</b></a><br>";
    echo $rss->limit($items[$i]['description'],100,true,3) . "<br>";
}

?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.