| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?php
- function clean_tag_content($text) {
- $text = html_entity_decode(utf8_encode($text), ENT_COMPAT | ENT_XHTML, 'utf-8');
- return htmlentities($text, ENT_COMPAT | ENT_HTML5 | ENT_SUBSTITUTE, 'utf-8');
- }
- function clean_html($html) {
- $html = preg_replace('/\s+/', ' ', $html);
- $in_tag = false;
- $stack = '';
- $cleaned = '';
- for ($i = 0, $l = strlen($html); $i < $l; $i++) {
- switch ($html[$i]) {
- case '<':
- $in_tag = true;
- $cleaned .= clean_tag_content($stack) . '<';
- break;
- case '>':
- $in_tag = false;
- $cleaned .= '>';
- $stack = '';
- break;
- default:
- if ($in_tag)
- $cleaned .= $html[$i];
- else
- $stack .= $html[$i];
- }
- }
- if (!$in_tag)
- $cleaned .= clean_tag_content($stack);
- //echo $cleaned . "\n\n";
- return $cleaned;
- }
- // Fetch details page
- assert(isset($_GET['id']));
- assert(is_numeric($_GET['id']));
- $url = 'http://www.tvgids.nl/programma/' . $_GET['id'];
- $page = file_get_contents($url);
- // Parse detailed description, preserving a selected set of HTML tags
- preg_match('/<div\s+id="prog-content">\s*(.*?)\s*<div\s+class="prog-functionbar">/s', $page, $m1);
- assert($m1);
- $description = strip_tags($m1[1], '<p><strong><em><b><i><font><a><span><img><br>');
- $description = str_replace('showVideoPlaybutton()', '', $description);
- $description = clean_html($description);
- //$description = preg_replace('/\s+/', ' ', $description);
- //$description = htmlentities($description, ENT_COMPAT | ENT_HTML5 | ENT_SUBSTITUTE, 'ISO-8859-1');
- //$description = str_replace(array('<', '>', '/'), array('<', '>', '/'), $description);
- // Parse properties list
- preg_match('/<ul\s+id="prog-info-content-colleft">\s*(.*?)\s*<\/ul>/s', $page, $m2);
- assert($m2);
- preg_match_all('/<li><strong>(\w+):<\/strong>(.*?)<\/li>/', $m2[1], $m3);
- assert($m3);
- $properties = array();
- foreach ($m3[1] as $i => $name)
- $properties[] = array('name' => $name, 'value' => $m3[2][$i]);
- header('Content-Type: application/json; charset=utf-8');
- echo json_encode(compact('description', 'properties'), JSON_UNESCAPED_SLASHES);
- ?>
|