Paul Dessert
Paul Dessert

Reputation: 6389

Using Regular Expression to block domain

I'm creating a blacklist and I want to block ALL variations of a given URL. For example,

www.google.com
google.com
google.com/maps
sub.google.com
google.com/dir/dir2/dir3

I know next to nothing about RegEx, so far I have this feeble attempt:

$blacklist = array(
'\.google.\\',
); 

Can someone help me out?

EDIT:

I'd also like to block ALL domain with specific extensions such as .me .xxx

Upvotes: 0

Views: 3067

Answers (2)

casper123
casper123

Reputation: 1766

If you ONLY want to block the domains then why not simply use strpos Its really good and faster then regex match for simple pattern matching.. Check out the link

$domain_name = "www.maps.google.com";
if(strpos($domain_name,"google") > 1)
    do sumthing here
else
    do sumthing

hope it helps :)

Upvotes: 0

Paul Bain
Paul Bain

Reputation: 4397

If you're just doing a pre_match you probably just need something along the lines of:

<?php
$subject = "somedomain";
$pattern = '/\.google\./';
if(preg_match($pattern, $subject)){
    die('blacklisted');
}
?>

Upvotes: 1

Related Questions