Friday, August 31, 2012

you tube url parsing uisng php and play on web page


/*
* YoutubeParser() PHP class
* @author: takien
* @version: 1.0
* @date: June 16, 2012
* URL: http://takien.com/864/php-how-to-parse-youtube-url-to-get-video-id-thumbnail-image-or-embed-code.php
*
* @param string $source content source to be parsed, eg: a string or page contains youtube links or videos.
* @param boolean $unique whether the return should be unique (duplicate result will be removed)
* @param boolean $suggested whether show suggested video after finished playing
* @param boolean $https whether use https or http, default false ( http )
* @param string $width width of embeded video, default 420
* @param string $height height of embeded video, default 315
* @param boolean $privacy whether to use 'privacy enhanced mode or not',
* if true then the returned Youtube domain would be youtube-nocookie.com
*/
class YoutubeParser{
var $source = '';
var $unique = false;
var $suggested = false;
var $https = false;
var $privacy = false;
var $width = 354;
var $height = 234;
function __construct(){
}
function set($key,$val){
return $this->$key = $val;
}
function getall(){
$return = Array();
$domain = 'http'.($this->https?'s':'').'://www.youtube'.($this->privacy?'-nocookie':'').'.com';
$size = 'width="'.$this->width.'" height="'.$this->height.'"';
preg_match_all('/(youtu.be\/|\/watch\?v=|\/embed\/)([a-z0-9\-_]+)/i',$this->source,$matches);
if(isset($matches[2])){
if($this->unique){
$matches[2] = array_values(array_unique($matches[2]));
}
foreach($matches[2] as $key=>$id) {
$return[$key]['id'] = $id;
$return[$key]['embedold'] = '<img class="mceItemMedia mceItemFlash" data-mce-json="{'video':{},'params':{'movie':null,'allowFullScreen':'true','allowscriptaccess':'flase','src':null},'object_html':'suggested?\'\':\'&amp;amp;rel=0\').\'\&quot;&amp;gt;suggested?\'\':\'&amp;amp;rel=0\').\'\&quot; type=\&quot;application/x-shockwave-flash\&quot; \'.$size.\' allowscriptaccess=\&quot;always\&quot; allowfullscreen=\&quot;true\&quot;&amp;gt;'}" height="240" src="http://bikashnayak.blog.com/wp-includes/js/tinymce/themes/advanced/img/trans.gif" width="320" />';<br />
}<br />
}
return $return[$key]['embedold'];
}
}
$youtube = new YoutubeParser;
$youtube->set('source','http://www.youtube.com/watch?v=R55e-uHQna0&feature=topvideos');
echo'
';
print_r($youtube->getall());
echo'
';
-------------------------------------------------------------
if you helpful my code please donate some few amount to
developing and free to post.
-------------------------------------------------------------

No comments:

Post a Comment