Skip to content

Instantly share code, notes, and snippets.

@esedic
Created March 16, 2025 16:58
Show Gist options
  • Save esedic/502be131b71a95572cb871a8963d4887 to your computer and use it in GitHub Desktop.
Save esedic/502be131b71a95572cb871a8963d4887 to your computer and use it in GitHub Desktop.
Get Youtube video ID from a string with PHP
<?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