Reputation: 3109
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.
Upvotes: 4
Views: 3578
Reputation: 75103
then you should just update your 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 simply use the video ID that comes from the Database...
if you are uploading the video ID from the hole link, use the URI
object 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 lack two things, the support for the ParseQueryString
part of the System.Web.HttpUtility
and the System.Collections.Specialized.NameValueCollection
So, why not just add that functionality yourself?
Because HttpUtility
is already a static
object you can't extend this, but you can easily create yourself something new:
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"];
Upvotes: 3
Reputation: 1049
Try using String.split() method:
String[] splitedUrl=preURL.split("v=",StringSplitOptions.None);
String[] splitedUrl2=splitedUrl[1].split('&');
String vidID=splitedUrl2[0];
Then you'll have that vidID.
Upvotes: 1
Reputation:
Why make it so complex?
Just use what already exists. Its a url. Its been here for 30 years try this
http://www.stev.org/post/2011/06/27/C-HowTo-Parse-a-URL.aspx
Upvotes: 3