details.php 2.1 KB

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