I used to embed YouTube video’s via a link I have in a database.
YouTube no longer allows me to embed in this fashion. Instead, I need to extract the video ID from the URL I have stored.
Here is an example of the link:
http://www.youtube.com/watch?v=72xSxQIRelc&feature=youtube_gdata
The ID I need to extract is 72xSxQIRelc. I know I could 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’ve seen some Regex stuff on here that does this. I’m wondering if my way is horribly inefficient, comparatively. I’m also not a regex kung fu wizard, so I’m not sure if the solutions I’m seeing are scalable if YouTube changes the URL structure (in this case, it’s hard coded to require v= as well as &feature).
My solution still seems pretty hackish to me. Is there a more efficient way of doing this?
Added
This app is wrapped in a silverlight solution, so I should mention that here.
One posted solution was to use HttpUtility.ParseQueryString. That looked like it was the answer, up until I discovered that Silverlight’s implementation of HttpUtility doesn’t include the ParseQueryString method.
then you should just update your database no?
Then you can simply use the video ID that comes from the Database…
if you are uploading the video ID from the hole link, use the
URIobject insteadSilverlight update
Silveright lack two things, the support for the
ParseQueryStringpart of theSystem.Web.HttpUtilityand theSystem.Collections.Specialized.NameValueCollectionSo, why not just add that functionality yourself?
Because
HttpUtilityis already astaticobject you can’t extend this, but you can easily create yourself something new:and then use: