class Youtube
{
private $url = null;
private $id;
public function __construct($url)
{
$this->url = $url;
$this->parseUrl($url);
}
protected function parseUrl($url)
{
if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match)) {
$this->id = $match[1];
}
else
{
throw new exception('Cannot find an ID!');
}
}
public function getVideo($width='560', $height='315')
{
$video = <<<EOF
<iframe width="$width" height="$height" src="//www.youtube-nocookie.com/embed/$this->id" frameborder="0" allowfullscreen></iframe>
EOF;
return $video;
}
}
$sistar = new Youtube('http://www.youtube.com/watch?v=JtVhwsACgTw');
print $sistar->getVideo();
$twoneone = new Youtube('http://www.youtube.com/watch?v=LUrUPzLm5SI');
print $twoneone->getVideo();
This is my first object-oriented script in PHP (I hope it's not too bad in general). I know I should do more validation and commenting within the code. Do you have any suggestions for improving the quality of the code?