Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Claushingebjerg 936 posts 2571 karma points
    Sep 22, 2014 @ 22:27
    Claushingebjerg
    0

    Extract Youtube ID from url

    Im looking to embed a youtube video from a parameter on a doc type.

    I want it to be as generic as possible, so the user doesn't have to worry about finding the exact ID but can just copy the entire url fromyoutube.

    So i need to extract the ID from the url

    https://www.youtube.com/watch?v=w59e20ijOpE

    That would mean getting 11 characters after the "=" character, resulting in: 

    w59e20ijOpE

    How would one do this in razor?

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Sep 22, 2014 @ 22:38
    Jeavon Leopold
    1

    How about something ilke this

       var youTubeId = Model.Content.GetPropertyValue<string>("myYouTubePropertyAlias").Split('=').Last()
    
  • Claushingebjerg 936 posts 2571 karma points
    Sep 22, 2014 @ 22:42
    Claushingebjerg
    1

    Thanks. Thats great. I fiddled around a bit and got this:

        string myString = CurrentPage.youtubeID;
        int charPositionStart = myString.IndexOf("=");
        int lengthOfText = 11;
        myString = myString.Substring(charPositionStart+1, lengthOfText); 

    That would make it work as well if the url doesnt stop with the ID, like:

    https://www.youtube.com/watch?v=p69z3zytU6I&list=LLzZhNDYY7C2h9-2oqcrziJQ

     

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Sep 22, 2014 @ 22:47
    Jeavon Leopold
    0

    This might be more robust

    var uri = new Uri(Model.Content.GetPropertyValue<string>("myYouTubePropertyAlias"));
    var query = HttpUtility.ParseQueryString(uri.Query);
    
    var youTubeId = query.Get("v");
    
Please Sign in or register to post replies

Write your reply to:

Draft