Created
March 16, 2025 16:58
-
-
Save esedic/502be131b71a95572cb871a8963d4887 to your computer and use it in GitHub Desktop.
Get Youtube video ID from a string with PHP
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 | |
abstract class Helper { | |
public static function getVidId($text) { | |
if(preg_match('~(?:https?://)?(?:www.)?(?:youtube.com|youtu.be|youtube-nocookie.com)/(?:watch\?v=|embed/)?([^"\?\s]+)~', $introtext, $match)) { | |
return $match[1]; // | |
} | |
} | |
} | |
$vidid = Helper::getVidId('https://www.youtube.com/watch?v=aqUo1Az6g0U'); | |
echo $vidid; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment