I'm trying to create a strict chat filter for those that choose the strict version. I would like to block all URLs except a few whitelisted ones (youtube, prntscr, facebook, etc) to prevent people from sending porn, IP grabbers, virus downloads, etc.
I know I could do this with a few extra lines of code, however is there a way to do this using a regular expression? I would like to have it check if the string contains a URL but the URL is not a whitelisted one (youtube.com for example).
I'm looking to implement this in both Python and PHP, but I only need the regex since I know how to simply use regular expressions in both languages.
Thanks
Edit: to be clear - this is for a strict mode on a chat system. The messages a user sends could be anything from "Hello" to "http://unsafelink.com go there!!"
Check the snippet below
$message = array(
"Dude I saw her on youtube",
"I just opened an account on youtuber.com",
"I'm watching an amazing prank, check this out youtube.com/gfsddfh784",
"Dude, isn't this girl forbidden.com/hot-chick/123 Mery from our school?",
"Take a look google.com?search=how%20to%20hack%20a%20wireless",
"Ask someone on stackoverflow.com :p",
"I found this great snippet on stackoverflow!",
"He's all day on xxx.net"
);
$url = '/(((https?:\/\/)?www)?\.?[a-z0-9]+\.[a-z0-9]+[a-z0-9\-\/?&#%=]+)/';
$whitelist = "/\b(youtube|stackoverflow|google|twitter|facebook|prntscr)\b/";
// check messages like this
foreach ($message as &$line){
if(preg_match($url, $line, $match)){
echo $match[0] , preg_match($whitelist, $match[0]) ? " -> Safe" : " -> Unsafe" , '<br />';
}
}
echo "<hr />";
// or like this
foreach ($message as &$line){
if(preg_match($url, $line, $match) && !preg_match($whitelist, $match[0])){
echo $match[0] . " -> Unsafe" . '<br />';
}
}
Output:
youtuber.com -> Unsafe
youtube.com/gfsddfh784 -> Safe
forbidden.com/hot-chick/123 -> Unsafe
google.com?search=how%20to%20hack%20a%20wireless -> Safe
stackoverflow.com -> Safe
xxx.net -> Unsafe
------------------------------------------------------------
youtuber.com -> Unsafe
forbidden.com/hot-chick/123 -> Unsafe
xxx.net -> Unsafe