opml_parser.inc 1.1 KB

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