Question

Vimeo share video link looks like:

http://vimeo.com/89491724

YouTube share video link looks like:

http://youtu.be/GioRM0kU5m0

I want people to enter this into my website and I'll embed it using their embed code and just replacing the last (id) part with their values (that gets stored in a DB).

I can parse these out by taking the last values after the last slash "/". I can tell what site they are from based on if "vimeo" is in the string. My question is, am I opening myself up to any security issues by doing this? Is there anything a person could do to exploit this system that anyone can think of?

Are there any libraries out there for validation that maybe I'm not aware of?

Was it helpful?

Solution

Yes I have a same problem few months ago and I have made this function to get video id for youtube and Vimeo..

Here is your Solution..

function Getvideoid(videolink)

var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = videolink.match(regExp);

  if (match && match[7].length == 11) 
    {
      alert("youtube video id : "+ match[7]);      
      return;
    }
    regExp = "vimeo\\.com/(?:.*#|.*/videos/)?([0-9]+)";
    match = videolink.match(regExp);
    if(match)
      {
       var videoid = videolink.split('/')[videolink.split('/').length - 1];
        alert("vimeo video id :"+videoid);
      }

 else 
  {
    alert("Unknown url");
  }

and here is JSBIN Link http://jsbin.com/zoxicoko/2/

OTHER TIPS

function parseVideo(url) {
    // - Supported YouTube URL formats:
    //   - http://www.youtube.com/watch?v=My2FRPA3Gf8
    //   - http://youtu.be/My2FRPA3Gf8
    //   - https://youtube.googleapis.com/v/My2FRPA3Gf8
    // - Supported Vimeo URL formats:
    //   - http://vimeo.com/25451551
    //   - http://player.vimeo.com/video/25451551
    // - Also supports relative URLs:
    //   - //player.vimeo.com/video/25451551

    url.match(/(http:|https:|)\/\/(player.|www.)?(vimeo\.com|youtu(be\.com|\.be|be\.googleapis\.com))\/(video\/|embed\/|watch\?v=|v\/)?([A-Za-z0-9._%-]*)(\&\S+)?/);

    if (RegExp.$3.indexOf('youtu') > -1) {
        var type = 'youtube';
    } else if (RegExp.$3.indexOf('vimeo') > -1) {
        var type = 'vimeo';
    }

    return {
        type: type,
        id: RegExp.$6
    };
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top