HTMLPurifier 4.4.0
/home/ezyang/Dev/htmlpurifier/library/HTMLPurifier/Filter/YouTube.php
Go to the documentation of this file.
00001 <?php
00002 
00003 class HTMLPurifier_Filter_YouTube extends HTMLPurifier_Filter
00004 {
00005 
00006     public $name = 'YouTube';
00007 
00008     public function preFilter($html, $config, $context) {
00009         $pre_regex = '#<object[^>]+>.+?'.
00010             'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s';
00011         $pre_replace = '<span class="youtube-embed">\1</span>';
00012         return preg_replace($pre_regex, $pre_replace, $html);
00013     }
00014 
00015     public function postFilter($html, $config, $context) {
00016         $post_regex = '#<span class="youtube-embed">((?:v|cp)/[A-Za-z0-9\-_=]+)</span>#';
00017         return preg_replace_callback($post_regex, array($this, 'postFilterCallback'), $html);
00018     }
00019 
00020     protected function armorUrl($url) {
00021         return str_replace('--', '-&#45;', $url);
00022     }
00023 
00024     protected function postFilterCallback($matches) {
00025         $url = $this->armorUrl($matches[1]);
00026         return '<object width="425" height="350" type="application/x-shockwave-flash" '.
00027             'data="http://www.youtube.com/'.$url.'">'.
00028             '<param name="movie" value="http://www.youtube.com/'.$url.'"></param>'.
00029             '<!--[if IE]>'.
00030             '<embed src="http://www.youtube.com/'.$url.'"'.
00031             'type="application/x-shockwave-flash"'.
00032             'wmode="transparent" width="425" height="350" />'.
00033             '<![endif]-->'.
00034             '</object>';
00035 
00036     }
00037 }
00038 
00039 // vim: et sw=4 sts=4