Wolf95 Posted September 2, 2011 Share Posted September 2, 2011 I'm trying to create a video system, and I have everything set, except the input, it integrates with youtube and I need to convert a link such as: >http://youtu.be/ty62YzGryU4< or > into: ty62YzGryU4 I have looked into all of the commands and I don't really understand how to get them to work properly, Do any of you know how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/246256-parsing-text/ Share on other sites More sharing options...
doddsey_65 Posted September 2, 2011 Share Posted September 2, 2011 there are many options for something like this so i will just throw one out there: $url = preg_replace('|http:\/\/|i', '[url=http://www.youtube.com/watch?v=ty62YzGryU4]http://www.youtube.com/watch?v=ty62YzGryU4[/url]', $url); if(preg_match('|watch|i', $url)) { $url_parts = explode('=', $url); $vid_code = $url_parts[1]; } else { $url_parts = explode('/', $url); $vid_code = $url_parts[1]; } first i removed the http from the url so the else statement doesnt throw up too many array keys. if the word watch is in the url then we split the url at the "=" sign which gives an array with 2 keys, the code is the second key. if the word watch isn't in the url then we split the url at the "/" (that's why i removed the http) and the code is the second key in the array Quote Link to comment https://forums.phpfreaks.com/topic/246256-parsing-text/#findComment-1264655 Share on other sites More sharing options...
Wolf95 Posted September 2, 2011 Author Share Posted September 2, 2011 Are the Brackets >< added by the forum software or did you add them yourself? Quote Link to comment https://forums.phpfreaks.com/topic/246256-parsing-text/#findComment-1264727 Share on other sites More sharing options...
doddsey_65 Posted September 2, 2011 Share Posted September 2, 2011 they were added by the forum. Didnt know they were there sorry. Quote Link to comment https://forums.phpfreaks.com/topic/246256-parsing-text/#findComment-1264728 Share on other sites More sharing options...
QuickOldCar Posted September 3, 2011 Share Posted September 3, 2011 Here's an embed I made and seems to work all instances. <?php //parse the host, no http:// returned function parseHOST($url){ $new_parse_url = str_ireplace(array("www.","mms://","rtsp://","http://","https://", "http://", "ftp://", "feed://"), "", trim($url)); $parsedUrl = @parse_url("http://$new_parse_url"); return strtolower(trim($parsedUrl['host'] ? $parsedUrl['host'] : array_shift(explode('/', $parsedUrl['path'], 2)))); } function embedYOUTUBE($url){ $parsed_url = parseHOST($url); //convert youtu.be shortened to normal if($parsed_url == "youtu.be"){ $vidid = end(explode("/",trim($url))); $url = "http://www.youtube.com/v/$vidid"; } //youtube media embed if(preg_match("/(youtube.com\/(watch?|v\/|v=))/i",$url)){ $url = str_ireplace(array("&autoplay=$vidid","?f=videos","?f=playlists","&app=youtube_gdata","&feature=related"),"",$url); $vidparser = parse_url($url); parse_str($vidparser[query], $query); $vidid = ($query['v']); if($vidid == ""){ $vidid = end(explode("/",$url)); } //clean parameters and get title $vidid = str_ireplace(array("&app=youtube_gdata","&autoplay=$vidid","&autoplay=1","&fs=1"),"",$vidid); $visit_url = "http://gdata.youtube.com/feeds/api/videos/".$vidid; $doc = new DOMDocument; @$doc->load($visit_url); $link_title = $doc->getElementsByTagName("title")->item(0)->nodeValue; if($link_title == ""){ $link_title = "Click to watch video."; } //show results on page echo "<div>"; echo "<h2><a href='$url' TARGET='_blank'>$link_title</a></h2>"; echo $vidid."<br />"; echo "<object style='height: 390px; width: 640px'> <param name='movie' value='http://www.youtube.com/v/$vidid&fs=1'> <param name='allowFullScreen' value='true'> <param name='allowScriptAccess' value='always'> <embed src='http://www.youtube.com/v/$vidid&fs=1' type='application/x-shockwave-flash' allowfullscreen='true' allowScriptAccess='always' width='640' height='390'></object>"; echo "</div>"; } else { return $url; } } //examples echo embedYOUTUBE("http://www.youtube.com/watch?v=EHtWe4_Fkok&feature=watch-now-button&wide=1"); echo embedYOUTUBE("http://www.youtube.com/CartooningGenius"); echo embedYOUTUBE("http://www.youtube.com/v/7JxfgId3XTs&fs=1"); echo embedYOUTUBE("http://youtu.be/jlgVLblVSu8"); echo embedYOUTUBE("http://www.youtube.com/watch?v=w5Beh6fEJVs&feature=topvideos_sports"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/246256-parsing-text/#findComment-1264966 Share on other sites More sharing options...
QuickOldCar Posted September 3, 2011 Share Posted September 3, 2011 slight modification to return the url and not embed the player if not a youtube video link. <?php //parse the host, no http:// returned function parseHOST($url){ $new_parse_url = str_ireplace(array("www.","mms://","rtsp://","http://","https://", "http://", "ftp://", "feed://"), "", trim($url)); $parsedUrl = @parse_url("http://$new_parse_url"); return strtolower(trim($parsedUrl['host'] ? $parsedUrl['host'] : array_shift(explode('/', $parsedUrl['path'], 2)))); } function embedYOUTUBE($url){ $parsed_url = parseHOST($url); if($parsed_url == "youtu.be" || $parsed_url == "youtube.com"){ //convert youtu.be shortened to normal if($parsed_url == "youtu.be"){ $vidid = end(explode("/",trim($url))); $url = "http://www.youtube.com/v/$vidid"; } //youtube media embed if(preg_match("/(youtube.com\/(watch?|v\/|v=))/i",$url)){ $url = str_ireplace(array("&autoplay=$vidid","?f=videos","?f=playlists","&app=youtube_gdata","&feature=related"),"",$url); $vidparser = parse_url($url); parse_str($vidparser[query], $query); $vidid = ($query['v']); if($vidid == ""){ $vidid = end(explode("/",$url)); } //clean parameters and get title $vidid = str_ireplace(array("&app=youtube_gdata","&autoplay=$vidid","&autoplay=1","&fs=1"),"",$vidid); $visit_url = "http://gdata.youtube.com/feeds/api/videos/".$vidid; $doc = new DOMDocument; @$doc->load($visit_url); $link_title = $doc->getElementsByTagName("title")->item(0)->nodeValue; if($link_title == ""){ $link_title = "Click to watch video."; } //show results on page echo "<div>"; echo "<h2><a href='$url' TARGET='_blank'>$link_title</a></h2>"; echo $vidid."<br />"; echo "<object style='height: 390px; width: 640px'> <param name='movie' value='http://www.youtube.com/v/$vidid&fs=1'> <param name='allowFullScreen' value='true'> <param name='allowScriptAccess' value='always'> <embed src='http://www.youtube.com/v/$vidid&fs=1' type='application/x-shockwave-flash' allowfullscreen='true' allowScriptAccess='always' width='640' height='390'></object>"; echo "</div>"; } else { return "<a href='$url'>$url</a>"; } } else { return "<a href='$url'>$url</a>"; } } //examples echo embedYOUTUBE("http://phpfreaks.com"); echo embedYOUTUBE("http://www.youtube.com/watch?v=EHtWe4_Fkok&feature=watch-now-button&wide=1"); echo embedYOUTUBE("http://www.youtube.com/CartooningGenius"); echo embedYOUTUBE("http://www.youtube.com/v/7JxfgId3XTs&fs=1"); echo embedYOUTUBE("http://youtu.be/jlgVLblVSu8"); echo embedYOUTUBE("http://www.youtube.com/watch?v=w5Beh6fEJVs&feature=topvideos_sports"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/246256-parsing-text/#findComment-1264971 Share on other sites More sharing options...
QuickOldCar Posted September 6, 2011 Share Posted September 6, 2011 I redid this a lot, all you need to do is use the $text value as whatever your text value may be. <?php //your text input from a post $text = "First lets do a hyperlink: <a href='http://www.youtube.com/watch?v=YaxKiZfQcX8'>Visit my youtube link</a> Some sample text with WWW.AOL.com. <br /> Here's a shortened link - youtu.be/ZzDm2vMHabE <br />http://www.youtube.com/watch?v=csgZ2b1bW2o and a cool user page is http://www.youtube.com/user/cometodoyourwill<br />Anyone use www.myspace.com? <br />Some people are nuts, look at this stargate link at http://www.youtube.com/watch?v=ZKoUm6z5SzU&feature=grec_index , like aliens exist or something. http://www.youtube.com/watch?v=sfN-7HczmOU&feature=grec_index and here's a secure site https://familyhistory.hhs.gov, unless you use curl or allow secure connections it will never get a title. <br /> This is a not valid site http://zzzzzzz and this is a dead site http://zwzwzwxzw.com.<br /> Lastly lets try an already made hyperlink and see what it does <a href='http://dynaindex.com'>dynaindex.com</a>"; //space anything that would get included in a link, add the space $text = str_ireplace(array("<br />","\n","\r"),array(" <br /> "," \n "," \r "),$text); $text = str_replace(" ", " ", $text); //explode the text by spaces $text_explode = explode(" ",$text); //loop replacing youtube links with embed codes, if not youtube embed return text foreach($text_explode as $words){ if (preg_match('~(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})~i', $words, $match)) { $vidid = $match[1];//the id $link = "http://youtu.be/$vidid";//short link to video //grab the title $visit_url = "http://gdata.youtube.com/feeds/api/videos/".$vidid; $doc = new DOMDocument; @$doc->load($visit_url); $link_title = $doc->getElementsByTagName("title")->item(0)->nodeValue; if($link_title == ""){ $link_title = "Click to watch video."; } //display a link echo "<h3><a href='$link' TARGET='_blank'>$link_title</a></h3>"; //embed the video echo "<div>"; echo "<object style='height: 390px; width: 640px'> <param name='movie' value='http://www.youtube.com/v/$vidid&fs=1'> <param name='allowFullScreen' value='true'> <param name='allowScriptAccess' value='always'> <embed src='http://www.youtube.com/v/$vidid&fs=1' type='application/x-shockwave-flash' allowfullscreen='true' allowScriptAccess='always' width='640' height='390'></object>"; echo "</div><br />"; } else { //keep the original words echo " $words "; } } ?> Here's the demo of this code. http://get.blogdns.com/test/embed-youtube2.php Quote Link to comment https://forums.phpfreaks.com/topic/246256-parsing-text/#findComment-1266144 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.