I was browsing through stackoverflow and found a great regex code HERE. There may be other methods for isolation a youtube video id but I chose to work with regex for learning purposes. The regex code with input1 (shown below) ignores everything after the & character. This wipes out the video id and therefore giving the incorrect or an empty id result. Why is the regex clearing everything after &?
Error:
Input1: http://www.youtube.com/watch?feature&v=317a815FLWQ
Result1: http//www.youtube.com/watch?feature
Normal:
Input2: http://www.youtube.com/watch?v=spDj54kf-vY&feature=g-vrec
Result2: http://www.youtube.com/watch?v=spDj54kf-vY
Regex Code (With original comments)
$text = preg_replace('~
# Match non-linked youtube URL in the wild. (Rev:20111012)
https?:// # Required scheme. Either http or https.
(?:[0-9A-Z-]+\.)? # Optional subdomain.
(?: # Group host alternatives.
youtu\.be/ # Either youtu.be,
| youtube\.com # or youtube.com followed by
\S* # Allow anything up to VIDEO_ID,
[^\w\-\s] # but char before ID is non-ID char.
) # End host alternatives.
([\w\-]{11}) # $1: VIDEO_ID is exactly 11 chars.
(?=[^\w\-]|$) # Assert next char is non-ID or EOS.
(?! # Assert URL is not pre-linked.
[?=&+%\w]* # Allow URL (query) remainder.
(?: # Group pre-linked alternatives.
[\'"][^<>]*> # Either inside a start tag,
| </a> # or inside <a> element text contents.
) # End recognized pre-linked alts.
) # End negative lookahead assertion.
[?=&+%\w-]* # Consume any URL (query) remainder.
~ix',
'<a href="http://www.youtube.com/watch?v=$1">YouTube link: $1</a>',
$text);
return $text;