1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* OPML Parser.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Parse OPML file.
|
10
|
*
|
11
|
* @param $raw
|
12
|
* File contents.
|
13
|
* @return
|
14
|
* An array of the parsed OPML file.
|
15
|
*/
|
16
|
function opml_parser_parse($raw) {
|
17
|
$feeds = $items = array();
|
18
|
$xml = @ new SimpleXMLElement($raw);
|
19
|
$feeds['title'] = (string)current($xml->xpath('//head/title'));
|
20
|
|
21
|
// @todo Make xpath case insensitive.
|
22
|
$outlines = $xml->xpath('//outline[@xmlUrl]');
|
23
|
foreach ($outlines as $outline) {
|
24
|
$item = array();
|
25
|
foreach ($outline->attributes() as $k => $v) {
|
26
|
if (in_array(strtolower($k), array('title', 'text', 'xmlurl'))) {
|
27
|
$item[strtolower($k)] = (string) $v;
|
28
|
}
|
29
|
}
|
30
|
|
31
|
// If no title, forge it from text.
|
32
|
if (!isset($item['title']) && isset($item['text'])) {
|
33
|
if (strlen($item['text']) < 40) {
|
34
|
$item['title'] = $item['text'];
|
35
|
}
|
36
|
else {
|
37
|
$item['title'] = trim(substr($item['text'], 0, 30)) . ' ...';
|
38
|
}
|
39
|
}
|
40
|
if (isset($item['title']) && isset($item['xmlurl'])) {
|
41
|
$items[] = $item;
|
42
|
}
|
43
|
}
|
44
|
$feeds['items'] = $items;
|
45
|
return $feeds;
|
46
|
}
|