View Single Post
Old 10-06-2008, 06:58 PM   #2 (permalink)
Wildhoney
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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);    

Attached Files
File Type: php YouTube.php (596 Bytes, 121 views)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote