Update YouTube embed code to new style, fixes #28
[htmlpurifier.git] / library / HTMLPurifier / Filter / YouTube.php
blob276d8362fa055a7f3cd61a125d6dcab8840a33e4
1 <?php
3 class HTMLPurifier_Filter_YouTube extends HTMLPurifier_Filter
6 /**
7 * @type string
8 */
9 public $name = 'YouTube';
11 /**
12 * @param string $html
13 * @param HTMLPurifier_Config $config
14 * @param HTMLPurifier_Context $context
15 * @return string
17 public function preFilter($html, $config, $context)
19 $pre_regex = '#<object[^>]+>.+?' .
20 '(?:http:)?//www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s';
21 $pre_replace = '<span class="youtube-embed">\1</span>';
22 return preg_replace($pre_regex, $pre_replace, $html);
25 /**
26 * @param string $html
27 * @param HTMLPurifier_Config $config
28 * @param HTMLPurifier_Context $context
29 * @return string
31 public function postFilter($html, $config, $context)
33 $post_regex = '#<span class="youtube-embed">((?:v|cp)/[A-Za-z0-9\-_=]+)</span>#';
34 return preg_replace_callback($post_regex, array($this, 'postFilterCallback'), $html);
37 /**
38 * @param $url
39 * @return string
41 protected function armorUrl($url)
43 return str_replace('--', '-&#45;', $url);
46 /**
47 * @param array $matches
48 * @return string
50 protected function postFilterCallback($matches)
52 $url = $this->armorUrl($matches[1]);
53 return '<object width="425" height="350" type="application/x-shockwave-flash" ' .
54 'data="//www.youtube.com/' . $url . '">' .
55 '<param name="movie" value="//www.youtube.com/' . $url . '"></param>' .
56 '<!--[if IE]>' .
57 '<embed src="//www.youtube.com/' . $url . '"' .
58 'type="application/x-shockwave-flash"' .
59 'wmode="transparent" width="425" height="350" />' .
60 '<![endif]-->' .
61 '</object>';
65 // vim: et sw=4 sts=4