details.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. function clean_tag_content($text) {
  3. return htmlentities($text, ENT_COMPAT | ENT_HTML5 | ENT_SUBSTITUTE, 'ISO-8859-1');
  4. }
  5. function clean_html($html) {
  6. $html = preg_replace('/\s+/', ' ', $html);
  7. $in_tag = false;
  8. $stack = '';
  9. $cleaned = '';
  10. for ($i = 0, $l = strlen($html); $i < $l; $i++) {
  11. switch ($html[$i]) {
  12. case '<':
  13. $in_tag = true;
  14. $cleaned .= clean_tag_content($stack) . '<';
  15. break;
  16. case '>':
  17. $in_tag = false;
  18. $cleaned .= '>';
  19. $stack = '';
  20. break;
  21. default:
  22. if ($in_tag)
  23. $cleaned .= $html[$i];
  24. else
  25. $stack .= $html[$i];
  26. }
  27. }
  28. if (!$in_tag)
  29. $cleaned .= clean_tag_content($stack);
  30. return $cleaned;
  31. }
  32. // Fetch details page
  33. assert(isset($_GET['id']));
  34. assert(is_numeric($_GET['id']));
  35. $url = 'http://www.tvgids.nl/programma/' . $_GET['id'];
  36. $page = file_get_contents($url);
  37. // Parse detailed description, preserving a selected set of HTML tags
  38. preg_match('/<div\s+id="prog-content">\s*(.*?)\s*<div\s+class="prog-functionbar">/s', $page, $m1);
  39. assert($m1);
  40. $description = strip_tags($m1[1], '<p><strong><em><b><i><font><a><span><img>');
  41. $description = str_replace('showVideoPlaybutton()', '', $description);
  42. $description = clean_html($description);
  43. //$description = preg_replace('/\s+/', ' ', $description);
  44. //$description = htmlentities($description, ENT_COMPAT | ENT_HTML5 | ENT_SUBSTITUTE, 'ISO-8859-1');
  45. //$description = str_replace(array('&lt;', '&gt;', '&sol;'), array('<', '>', '/'), $description);
  46. // Parse properties list
  47. preg_match('/<ul\s+id="prog-info-content-colleft">\s*(.*?)\s*<\/ul>/s', $page, $m2);
  48. assert($m2);
  49. preg_match_all('/<li><strong>(\w+):<\/strong>(.*?)<\/li>/', $m2[1], $m3);
  50. assert($m3);
  51. $properties = array();
  52. foreach ($m3[1] as $i => $name)
  53. $properties[] = array('name' => $name, 'value' => $m3[2][$i]);
  54. echo json_encode(compact('description', 'properties'), JSON_UNESCAPED_SLASHES);
  55. ?>