-
-
Save beatwiz/f1d95582d58b51a41c1e723611d15f83 to your computer and use it in GitHub Desktop.
Extract YouTube ID from URL
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 | |
/** | |
* Fetch YouTube ID from URLs | |
* | |
* @param string $url | |
* @return string | |
*/ | |
function ja_youtube_id( $url ){ | |
$regex = '~(?:http|https|)(?::\/\/|)(?:www.|)(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/ytscreeningroom\?v=|\/feeds\/api\/videos\/|\/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{11})[a-z0-9;:@#?&%=+\/\$_.-]*~i'; | |
$id = preg_replace( $regex, '$1', $url ); | |
return $id; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment