$link = "https://www.youtube.com/watch?v=sOnqjkJTMaA";
$youtube_ID = get_youtube_ID( $link );
echo $youtube_ID;
<iframe width="560" height="315" src="https://www.youtube.com/embed/<?php echo $youtube_ID; ?>" frameborder="0" allowfullscreen></iframe>-
-
Save vaporic/7fdd2ff5b99f56f5bbe4b72f7b9d4cba to your computer and use it in GitHub Desktop.
Get YouTube url ID
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Get YouTube url ID | |
| * | |
| * @var string $link [ https, m., gaming., www., youtube.com gaming.youtube.com, youtu.be ] | |
| * @return array $youtube URL && ID | |
| */ | |
| function get_youtube_ID( $link ){ | |
| if( !isset( $link ) || empty( $link ) ) | |
| return; | |
| preg_match( '/(?:https?:\/\/|www\.|gaming\.|m\.|^)youtu(?:be\.com\/watch\?(?:.*?&(?:amp;)?)?v=|\.be\/)([\w\-]+)(?:&(?:amp;)?[\w\?=]*)?/', $link, $ID ); | |
| $YouTube_ID = isset( $ID[1] ) ? $ID[1] : false; | |
| return $YouTube_ID; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment