function youtube($string) { return preg_replace( '#(http://(www.)?youtube.com)?/(v/|watch\?v\=)([-|~_0-9A-Za-z]+)&?.*?#i', '<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/$4" frameborder="0" allowfullscreen></iframe>', $string ); } PHP: This is supposed to take the youtube URL and auto-embed it. The URL should be replaced the the embed code and the rest of the text should remain untouched. Embedding works like a charm, I just need to get rid of the whole URL. There seem to be two bugs: 1. youtube('http://www.youtube.com/watch?v=8uTThzrBYwg&feature=player_embedded'); // *embedded video*&feature=player_embedded (or amp;feature=player_embedded) PHP: That part of the URL should also be gone... 2. echo youtube('http://youtube.com/watch?v=8uTThzrBYwg&feature=player_embedded'); // www.youtube.com *embedded video* amp;feature=player_embedded PHP: Any help would be appreciated!
This works fine for me in a test.php file removing &feature=player_embedded <?php function youtube($string) { return preg_replace( '#(http://(www.)?youtube.com)?/(v/|watch\?v\=)([-|~_0-9A-Za-z]+)&feature=player_embedded#i', '<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/$4" frameborder="0" allowfullscreen></iframe>', $string ); } echo youtube('http://www.youtube.com/watch?v=8uTThzrBYwg&feature=player_embedded'); ?> PHP:
Thanks, but I see you added the variable in the regex. That was a mere example - the variables can have many other names and there can be more than one.
Ok so how about this way, getting the id first then position it into the iframe url <?php function youtube($string) { preg_match('#(v\/|watch\?v=)([\w\-]+)#', $string, $match); echo "<iframe title=\"YouTube video player\" width=\"480\" height=\"390\" src=\"http://www.youtube.com/embed/$match[2]\" frameborder=\"0\" allowfullscreen></iframe>"; } youtube('http://www.youtube.com/watch?v=8uTThzrBYwg&feature=player_embedded'); ?> PHP:
Now the problem is that the text gets wiped also youtube('check out this video http://www.youtube.com/watch?v=8uTThzrBYwg&feature=player_embedded please'); Only the embedded video is displayed. The text should remain untouched.
Are you making this up as we go along please show us the full source of your code on how you get this 'check out this video http://www.youtube.com/watch?v=8uTThzrBYwg&feature=player_embedded please'
Actually that was my code. It didn't display the function name you tube for some reason, so lets call it yout yout ('check out this video http://www.youtube.com/watch?v=8uTTh...layer_embedded please'); // returns only the embedded video // should return check out this video *embedded video* please PHP:
Maybe like this ? <?php function youtube($string) { preg_match('#(v\/|watch\?v=)([\w\-]+)#', $string, $match); $title = "Check out this video"; $comment = "Please"; echo "$title<br /> <iframe title=\"YouTube video player\" width=\"480\" height=\"390\" src=\"http://www.youtube.com/embed/$match[2]\" frameborder=\"0\" allowfullscreen></iframe><br />$comment"; } youtube('http://www.youtube.com/watch?v=8uTThzrBYwg&feature=player_embedded'); ?> PHP:
Hehe.. Man. I love that you are trying help, but the goal is to automatically embed youtube videos from users input. "Check out this video" was just an example what an user might say. It could be anything. The point being that only the URL should be replaced with the embedded video (and whatever variables are at the end of it), and the rest of the text should not be touched.
I understand now... but your not telling us how they input stuff, are you using $_POST[title] $_POST etc.. [php] $title = $_POST['tit...If I'm still way off the mark, I'll give up:)
There is only one variable and that is the $string. It's all crammed in there. Just like a forum post with an URL to a youtube video in it. My goal is to auto-embed that URL without touching the rest of the text. Thanks for trying to help, I appreciate it.
This seems to work, first we get the id, then we remove the youtube url altogether. You can add more symbols in here is required [=a-z0-9&_] <?php function youtube($string) { preg_match('#(v\/|watch\?v=)([\w\-]+)#', $string, $match); return preg_replace( '#(http://www\.youtube\.com/watch\?[=a-z0-9&_]+)#i', "<iframe title=\"YouTube video player\" width=\"480\" height=\"390\" src=\"http://www.youtube.com/embed/$match[2]\" frameborder=\"0\" allowfullscreen></iframe>", $string); } echo youtube('check out this video http://www.youtube.com/watch?v=8uTThzrBYwg&feature=player_embedded please'); ?> PHP:
That works like a charm. Except there is one small bug left in there. It doesn't work if the url only has http:// or www. It only seems to work with http://www. This one will probably take me several hours to figure out as I'm a total regex noob, but feel free to help me out.
This should solve that, its just like your first code <?php function youtube($string) { preg_match('#(v\/|watch\?v=)([\w\-]+)#', $string, $match); return preg_replace( '#(http://(www.)?youtube\.com/watch\?[=a-z0-9&_]+)#i', "<iframe title=\"YouTube video player\" width=\"480\" height=\"390\" src=\"http://www.youtube.com/embed/$match[2]\" frameborder=\"0\" allowfullscreen></iframe>", $string); } echo youtube('check out this video http://youtube.com/watch?v=8uTThzrBYwg&feature=player_embedded please'); ?> PHP: