degeaba degeaba - 2 years ago 167
PHP Question

Regex: Scrub a YouTube URL within a string, leaving only the YouTube video code

I have a text that contains a YouTube URL. I need to remove all portions of the link, except for the YouTube video code. The URL may be surrounded by blank space or nothing; no non-blank characters will adjoin the URL.


$txt = "This text contain this link: and so on..."


$pattern = '#(?<=v=|v\/|vi=|vi\/|\/)[a-zA-Z0-9_-]{11}#';
preg_match_all($pattern, $txt, $matches);


[0] = "This text contain this link b8ri14rw32c and so on..."

Answer Source

You can try this pattern to match:


There is exactly one capture in this expression, and it's for the YouTube video code. This capture can be used with a regex replace to replace the entire link text with just the captured video code.

This regex will work with these format YouTube URLs:

Other YouTube URL formats have not been tested, but could easily be supported if needed.

This PHP code will test this regexp replacement using preg_replace:

$txt = "This text contain this link: and so on..."
$pattern = "https:\/\/(?:www.)?youtu(?:be\.com|\.be)\/(?:watch\?vi?[=\/])?(\w{11})(?:&\w+=[^&\s]*)*"
$text = preg_replace($pattern, '\1', $txt);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download