Search the Community
Showing results for tags 'regexurl'.
-
So I am working on a site which take's people's YouTube links and automatically embeds them into the website. The site's purpose isn't the playback of the videos rather the video thumbnail itself. I'm not saying that the site does not use the <iframe> tag to embed the video but it doesn't matter if the videos do play or not depending on things like Vevo or UMG rights etc... My problem is, I don't know how to take a full youtube url that a person enters and place it within a container that is an iframe, the url of course goes into the src=" " field. I found something that supposedly does this but I don't know how to apply it. I don't know how to take the link which comes in from a form and then before storing it into the database, apply a function to it to modify the string (add the container) The guy who wrote the code below said something easy yet I don't understand it... "all you have to do is parse the string then call the function" Can someone first answer, is this code actually doing what I want to do or do I misunderstand it? My understanding is, once a link is somehow accessed by the function autolink($string), it then first fixes the link to be sure it is proper, then if it is proper ($match), it is placed within the <iframe> container which happens to be an embed object. Please ignore my syntax so how do I take that string url link entered by the user on the form, then modify it, store it and retrieve to be displayed in an iframe? If you want to see what exactly I am doing, this is the site I am trying to make it on. You can see my other entries already which are just tests, I will empty the database once I get this thing running. It seems so simple but my ignorance hinders my progress. http://jchive.com/comments.html Thanks for any help. This is the link where the code came from : http://stackoverflow.com/questions/412467/how-to-embed-youtube-videos-in-php because it isn't the only code on the site, I will post the code: echo '<div id="chat_message">'.autolink($string).'</div>'; /****************Function to include****************/ <?php function autolink($string){ // force http: on www. $string = str_ireplace( "www.", "http://www.", $string ); // eliminate duplicates after force $string = str_ireplace( "http://http://www.", "http://www.", $string ); $string = str_ireplace( "https://http://www.", "https://www.", $string ); // The Regular Expression filter $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; // Check if there is a url in the text $m = preg_match_all($reg_exUrl, $string, $match); if ($m) { $links=$match[0]; for ($j=0;$j<$m;$j++) { if(substr($links[$j], 0, 18) == 'http://www.youtube'){ $string=str_replace($links[$j],'<a href="'.$links[$j].'" rel="nofollow" target="_blank">'.$links[$j].'</a>',$string).'<br /><iframe title="YouTube video player" class="youtube-player" type="text/html" width="320" height="185" src="http://www.youtube.com/embed/'.substr($links[$j], -11).'" frameborder="0" allowFullScreen></iframe><br />'; }else{ $string=str_replace($links[$j],'<a href="'.$links[$j].'" rel="nofollow" target="_blank">'.$links[$j].'</a>',$string); } } } return ($string); } ?>