Skip to content

Instantly share code, notes, and snippets.

@avuenja
Created May 27, 2015 20:20
Show Gist options
  • Save avuenja/8b6c74e37a0336f8015a to your computer and use it in GitHub Desktop.
Save avuenja/8b6c74e37a0336f8015a to your computer and use it in GitHub Desktop.
link youtube automatico
<?php
$video = "https://www.youtube.com/watch?v=1tnYpkt5G2g"; // $video recebe o valor da url do youtube (link)
preg_match('/(v\/|\?v=)([^&?\/]{5,15})/', $video, $x); // Faz o preg_match para retirar caracters
?>
<!-- Exibe o video -->
<object width="560" height="315">
<param name="movie" value="//www.youtube.com/v/<?php print end($x); ?>?hl=pt_BR&amp;version=3&amp;rel=0"></param>
<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>
<embed src="//www.youtube.com/v/<?php print end($x); ?>?hl=pt_BR&amp;version=3&amp;rel=0" type="application/x-shockwave-flash" width="560" height="315" allowscriptaccess="always" allowfullscreen="true"></embed>
</object>
@avuenja
Copy link
Author

avuenja commented Aug 16, 2017

$searchText = 'link do youtube';
$result = preg_replace('/(?:https?:\/\/)?(?:www\.)?youtu(?:\.be|be\.com)\/(?:watch\?v=)?([\w-]{10,})/m', '$1', $searchText);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment