/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] RegEx Problem for youtube links

this is what I have so far:

[code=html]/(http://www.youtube.com/watch?v=[a-zA-Z0-9]+)/[/code]

which gets [url]http://www.youtube.com?v=abcABC123[/url]

but if the link is [url]http://www.youtube.com/watch?v=abcABC123&feature=related[/url], it does not include &feature=related

this is the full code I am using it in:

[code=php]
function youtubeRender($uri) {

return “<a href=’$uri’>$uri</a>”;
}

function getYoutubeLink($string){
$regex = “/(http://www.youtube.com/watch?v=[a-zA-Z0-9]+)/”;
$string = preg_replace_callback($regex, create_function(‘$matches’, ‘return youtubeRender($matches[0]);’), $string);
return $string;
}

// this link work OK
echo getYoutubeLink(“http://www.youtube.com/watch?v=abcABC123 this video is cool”);

// this link doesn’t include “&feature=related” into the rendered link
echo getYoutubeLink(“http://www.youtube.com/watch?v=abcABC123&feature=related this video is cool”);
[/code]

I haven’t really used Regular Expressions much, so If anyone with more knowledge than me can get it working, It would be much appreciated ?

Thanks

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@vinny619authorNov 24.2011 — got it, not sure if its perfect, but it works for any youtube link, including "youtu.be" links

[code=html]/http://w{0,3}.?(youtube|youtu).[a-zA-Z]+(.?[a-zA-Z]+){0,3}(/watch?v=|/)([a-zA-Z0-9]){0,11}((&[a-z]+=[a-z]+)+)?/[/code]
×

Success!

Help @vinny619 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...