Analysis URL for Video ID

I used the embedded YouTube video through the link in the database.

YouTube no longer lets me embed this way. Instead, I need to extract the video id from the URL I saved.

Here is an example link:

http://www.youtube.com/watch?v=72xSxQIRelc&feature=youtube_gdata 

The identifier I need to extract is 72xSxQIRelc . I know I can do something like:

 string vidID = preURL.Substring(preURL.LastIndexOf("v=") + 2, preURL.Substring(preURL.LastIndexOf("v=") + 2).Length - preURL.Substring(preURL.LastIndexOf("&feature")).Length) 

I saw some regex things here that do this. I wonder if my path is terribly inefficient, comparatively. I am also not a master of reungx kung fu, so I am not sure that the solutions I see are scalable if YouTube changes the structure of the URL (in this case it is hardcoded to require v =, as well as a function).

My solution still seems pretty hacks to me. Is there a more efficient way to do this?


Added

This application is wrapped in a silverlight solution, so I should mention this.

One published solution is to use HttpUtility.ParseQueryString. It seems like this was the answer until I found that the Silverlight implementation for HttpUtility does not include the ParseQueryString method.

+4
source share
3 answers

then you should just upgrade the database no ?

 UPDATE [VideosTable] SET video_url = SUBSTRING( video_url, CHARINDEX('v=', video_url) + 2, CHARINDEX('&', video_url, CHARINDEX('v=', video_url)) - (CHARINDEX('v=', video_url) + 2) ) WHERE video_url LIKE '%youtube.com%'; 

Then you can just use the video id that comes from the database ...

if you are loading the video id from the hole link, use the URI instead

 Uri youtube = new Uri("http://www.youtube.com/watch?v=72xSxQIRelc&feature=youtube_gdata"); string videoId = HttpUtility.ParseQueryString(youtube.Query)["v"]; 

Silverlight Update

Silveright is missing two things, support for the ParseQueryString part of System.Web.HttpUtility and System.Collections.Specialized.NameValueCollection

So, why not just add this functionality yourself?

Since HttpUtility already a static object, you cannot extend it, but you can easily create something new for yourself:

 public static class MyHttpUtilities { public static System.Collections.Generic.Dictionary<string, string> ParseQueryString(this string queryString) { System.Collections.Generic.Dictionary<string, string> r = new Dictionary<string, string>(); // remove extra noise queryString = queryString.TrimStart('?').Replace("amp;", ""); // split up and fill up Dictionary foreach (string s in queryString.Split('&')) { if (s.Contains('=')) { string[] par = s.Split('='); r.Add(par[0], par[1]); } } return r; } } 

and then use:

 string videoId = MyHttpUtilities.ParseQueryString(youtube.Query)["v"], feature = MyHttpUtilities.ParseQueryString(youtube.Query)["feature"]; 
+3
source

Why is it so hard?

Just use what already exists. His url. It has been here for 30 years, try this

http://www.stev.org/post/2011/06/27/C-HowTo-Parse-a-URL.aspx

+3
source

Try using the String.split () method:

  String[] splitedUrl=preURL.split("v=",StringSplitOptions.None); String[] splitedUrl2=splitedUrl[1].split('&'); String vidID=splitedUrl2[0]; 

Then you will have this vidID.

+1
source

All Articles