Advertisement:

Author Topic: Need help with a Regular Expression for YouTube links....  (Read 5322 times)

Offline dougiefresh

  • SMF Hero
  • ******
  • Posts: 2,358
    • XPtsp.com Community
Need help with a Regular Expression for YouTube links....
« on: February 16, 2015, 04:54:30 PM »
I've got a mod called Yet Another YouTube BBCode Tag that I'm having problems with.  It's the regular expression that detects if the link passed is a valid YouTube link.  The code detects these URLs correctly:
Code: [Select]
http://youtu.be/fA4cphzsjn8
http://www.youtube.com/embed/fA4cphzsjn8
http://www.youtube.com/watch?v=fA4cphzsjn8
http://www.youtube.com/v/fA4cphzsjn8
http://www.youtube.com/e/fA4cphzsjn8
http://www.youtube.com/p/fA4cphzsjn8
http://www.youtube.com/?v=fA4cphzsjn8
but not these:
Code: [Select]
http://www.youtube.com/user/username#p/u/11/fA4cphzsjn8
http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/0/fA4cphzsjn8
http://www.youtube.com/watch?feature=player_embedded&v=fA4cphzsjn8
http://www.youtube.com/?feature=player_embedded&v=fA4cphzsjn8

This is the current code that the next version of this mod will be using, which I copied and have modified from the Youtube I.D parsing for new URL formats page:
Code: [Select]
function parse_yturl($url)
{
$pattern = '#^(?:https?://)?';    # Optional URL scheme. Either http or https.
$pattern .= '(?:www\.)?';         #  Optional www subdomain.
$pattern .= '(?:';                #  Group host alternatives:
$pattern .=   'youtu\.be/';       #    Either youtu.be,
$pattern .=   '|youtube\.com';    #    or youtube.com
$pattern .=   '(?:';              #    Group path alternatives:
$pattern .=     '/e/';            #      or /e/,
$pattern .=     '|/embed/';       #      Either /embed/,
$pattern .=     '|/v/';           #      or /v/,
$pattern .=     '|/\?v=';         #      or /?v=
$pattern .=     '|/watch\?v=';    #      or /watch?v=,   
> $pattern .=     '|/\?.+&v=';      #      or /?v=
> $pattern .=     '|/watch\?.+&v='; #      or /watch?other_param&v=
$pattern .=   ')';                #    End path alternatives.
$pattern .= ')';                  #  End host alternatives.
$pattern .= '([\w-]{11})';        # 11 characters (Length of Youtube video ids).
$pattern .= '(?:.+)?$#x';         # Optional other ending URL parameters.
preg_match($pattern, $url, $matches);
return (isset($matches[1])) ? $matches[1] : false;
}
The lines starting with the > character are the parts of the regular expression that I can't get to work.  I admit that I'm not very good at regular expressions, but I'm hoping someone can help me out.

Thanks in advance for your assistance....

EDIT: The parts of the expression that start with > are supposed to filter out everything between ? and &v=.  Hope this helps someone....
« Last Edit: February 16, 2015, 05:15:46 PM by dougiefresh »
NOTICE:  My website is back up!

My Free Simple Machines mods on XPtsp.com and Simple Machines Customization Site!
My mods are being uploaded and are available at GitHub as well!

Offline karavan2

  • Semi-Newbie
  • *
  • Posts: 27
  • Gender: Male
Re: Need help with a Regular Expression for YouTube links....
« Reply #1 on: February 16, 2015, 05:35:42 PM »
Hello dear dougiefresh, I've been watching your mod, and wrote in the topic further links that did not work http://www.simplemachines.org/community/index.php?topic=531060.msg3786649#msg3786649 My good friend and colleague Sapozhnik has introduced an interesting idea, which, with his permission, I will tell you a PM. Thank you for what you are doing such a good free mpd, you do SMF more simply and a nice in work.

Offline kelvincool

  • Jr. Member
  • **
  • Posts: 212
Re: Need help with a Regular Expression for YouTube links....
« Reply #2 on: February 16, 2015, 06:54:27 PM »
These two already work for me   ???

http://www.youtube.com/watch?feature=player_embedded&v=fA4cphzsjn8
http://www.youtube.com/?feature=player_embedded&v=fA4cphzsjn8

There's no code to take care of these two is there?

http://www.youtube.com/user/username#p/u/11/fA4cphzsjn8
http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/0/fA4cphzsjn8

Maybe like this?

Code: [Select]
$pattern .=     '|/user/.+\#.+/'; #      or /user/username#p/u/11/
$pattern .=     '|/.+\#.+/';      #      or /sandalsResorts#p/c/54B8C800269D7C1B/0/

Offline dougiefresh

  • SMF Hero
  • ******
  • Posts: 2,358
    • XPtsp.com Community
Re: Need help with a Regular Expression for YouTube links....
« Reply #3 on: February 17, 2015, 05:14:23 PM »
These two already work for me   ???

http://www.youtube.com/watch?feature=player_embedded&v=fA4cphzsjn8
http://www.youtube.com/?feature=player_embedded&v=fA4cphzsjn8
When tested on my localhost, my mod still reports that those are invalid links....  :-[  Keep in mind that the code I'm showing is the code I'm using....

There's no code to take care of these two is there?

http://www.youtube.com/user/username#p/u/11/fA4cphzsjn8
http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/0/fA4cphzsjn8

Maybe like this?

Code: [Select]
$pattern .=     '|/user/.+\#.+/'; #      or /user/username#p/u/11/
$pattern .=     '|/.+\#.+/';      #      or /sandalsResorts#p/c/54B8C800269D7C1B/0/
That takes care of those particular situations....  Thanks!
« Last Edit: February 17, 2015, 05:26:16 PM by dougiefresh »
NOTICE:  My website is back up!

My Free Simple Machines mods on XPtsp.com and Simple Machines Customization Site!
My mods are being uploaded and are available at GitHub as well!

Offline dougiefresh

  • SMF Hero
  • ******
  • Posts: 2,358
    • XPtsp.com Community
Re: Need help with a Regular Expression for YouTube links....
« Reply #4 on: February 17, 2015, 08:06:14 PM »
@karavan2: Thanks for that piece of code you sent via PM.  I've integrated the relevant sections into the function and now it works!!  Thank you very much!
NOTICE:  My website is back up!

My Free Simple Machines mods on XPtsp.com and Simple Machines Customization Site!
My mods are being uploaded and are available at GitHub as well!

Offline karavan2

  • Semi-Newbie
  • *
  • Posts: 27
  • Gender: Male
Re: Need help with a Regular Expression for YouTube links....
« Reply #5 on: February 18, 2015, 02:30:13 AM »
@karavan2: Thanks for that piece of code you sent via PM.  I've integrated the relevant sections into the function and now it works!!  Thank you very much!
I am very glad to help you. The idea of how to do it is owned by Sapozhnik, if you have questions, ask him to PM, it is very good specialist and humble man. Good Luck!

Offline dougiefresh

  • SMF Hero
  • ******
  • Posts: 2,358
    • XPtsp.com Community
Re: Need help with a Regular Expression for YouTube links....
« Reply #6 on: February 19, 2015, 07:32:50 PM »
Oops...  This topic isn't quite solved yet.  karavan2 notified me of another link that this regular expression doesn't cover.  It has to do with the contents of the URL that is AFTER the video ID.

For example, this isn't a valid YouTube URL according to the function given:
Code: [Select]
https://www.youtube.com/watch?feature=player_embedded&v=WlobIDJyyOs&x-yt-cl=85114404&x-yt-ts=1422579428Right now, my function uses the following code as a last resort detection method:
Code: [Select]
// LAST RESORT: If we still have no result, detect the video ID by parsing the URL:
if (empty($result))
{
parse_str(parse_url(str_replace('&', '&', $url), PHP_URL_QUERY), $out);
$result = (isset($out['v']) ? 'v/' . $out['v'] : false);
}
which does get the v variable from the URL, but I'm wondering if there is someway to rewrite the expression to filter that out....

The line in the expression that deals with this is marked with a >:
Code: [Select]
$pattern .= '([\w-]{11})';        # 11 characters (Length of Youtube video ids).
> $pattern .= '(?:.+)?$#x';         # Optional other ending URL parameters.
Any ideas?  Assistance is much appreciated!  Thanks in advance!
NOTICE:  My website is back up!

My Free Simple Machines mods on XPtsp.com and Simple Machines Customization Site!
My mods are being uploaded and are available at GitHub as well!