TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Regex assistance {Thank you, advance} (http://www.talkphp.com/advanced-php-programming/3450-regex-assistance-thank-you-advance.html)

codefreek 10-06-2008 05:23 PM

Regex assistance {Thank you, advance}
 
Hello, i am working on a kind of link site, you can share youtube links. In basic, it is like this you login as a user and upload a link or multi links. So the problem is that i want to validate that it is a youtube link.

all info you can provide on this would be great, and please do not share regex reference links or so beacuse it wont get me anywhere as its hard to understand :P oh well..

Thank you in advance!

^^

Wildhoney 10-06-2008 06:58 PM

1 Attachment(s)
There are obviously people who are better than me with regular expressions, but here's my attempt, and it appears to work well:

PHP Code:

function is_youtube_link($szLink)
{
    return (bool) 
preg_match('~http://(?:.+?\.)?youtube\.com.*~i'$szLink);
}

$aLinks = array
(
    
'http://www.google.com/',
    
'http://uk.youtube.com/',
    
'http://www.yahoo.com/youtube.com',
    
'http://www.youtube.com',    
    
'http://youtube.com'    
);

foreach (
$aLinks as $szLink)
{
    
$szIsOrIsnt is_youtube_link($szLink) ? '<font style="color: green">is</font>' '<font style="color: red">is not</font>';
    
printf('<a href="%1$s">%1$s</a> <strong>%2$s</strong> a valid YouTube link.<br />'$szLink$szIsOrIsnt);    



codefreek 10-06-2008 07:30 PM

thank you this is what i came up with '~^http://[a-z]{2,3}\.youtube\.com/\?v=[\w-]+(?:&feature=related)?$~i' for like 5 hours ago but the one you made seems better,
gah regex.. :P

masfenix 10-21-2008 07:26 PM

You have to also take in account that my youtube links end with .ca instead of .com.


All times are GMT. The time now is 04:37 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0