André Ferraz
André Ferraz

Reputation: 1521

Check if website is in the blocked list

Am creating a class that handles redirection to exterior locations. I created a method called isBlocked which returns a boolean result. This function works as follows:

It loops through the $_blocked variable and compares is the input string is found, it it is it returns true otherwise false. Bellow is the actual incase my explanation is not enough.

public static function isBlocked($location)
{
     for($i = 0; $i < sizeof(self::$_blocked); $i++)
     {
         if(self::$_blocked[$i] === $location)
         {
             return true;
         }
         else 
         {
             return false;
          }
     }
}

This works perfectly, the problem comes when for example, lets say "google.com" is on the list and the input is "www.google.com", it will return false. This is obvious since am using the identical operator ===.

Question is the following: Is there any function in PHP that work with URLS apart from url_encode & url_decode? I obviously can create a regular expression but let me know if there is any other approach to this.

Thanks in advance.

Upvotes: 2

Views: 247

Answers (4)

Jared Farrish
Jared Farrish

Reputation: 49208

It occurs to me that the match is backwards, since it's a many to one, with the one matching a (sub)part of any in the stack. So, in that case we can use a strpos() (in lieu of a regular expression):

function find_reverse_match($needle, $haystack) {
    $i = 0;
    $c = count($haystack);

    while ($i < $c) {
        if (strpos($needle, $haystack[$i++]) !== false) {
            return true;
        }
    }

    return false;
}

http://codepad.viper-7.com/69ot4O

Upvotes: 2

cn0047
cn0047

Reputation: 17071

i just take example: Php Search_Array using Wildcard that wrote @JaredFarrish and adopt for this particular case:

<?php

function isBlocked($location)
{
    $blocked = [
        'google.com',
        'www.google.com',
        'play.google.com',
        'developers.google.com',
    ];
    $matches = preg_grep("/^(www)?$location$/", $blocked);
    if (empty($matches)) {
        return false;
    } else {
        return true;
    }
}

print isBlocked('play.google.com'); // 1
print isBlocked('www.google.com'); // 1
print isBlocked('google.com'); // 1
print isBlocked('stackoverflow.com');  // 0

And the answer: you should write regular expression)

Upvotes: 1

Danijel
Danijel

Reputation: 12689

There is no php function to replace only first occurrence in a string, so preg_replace() and in_array() will do the job:

$blocked = array(
    'www.google.com',
    'yahoo.com'
);

var_dump( in_array( preg_replace( '/^www\./', '', 'google.com' ), preg_replace( '/^www\./', '', $blocked ) ) ); // true
var_dump( in_array( preg_replace( '/^www\./', '', 'www.yahoo.com' ), preg_replace( '/^www\./', '', $blocked ) ) );  // true
var_dump( in_array( preg_replace( '/^www\./', '', 'www.facebook.com' ), preg_replace( '/^www\./', '', $blocked ) ) );   // false

Upvotes: 1

user4655002
user4655002

Reputation:

Is it possible for you to string replace the www.?

public static function isBlocked($location)
{
     for($i = 0; $i < sizeof(self::$_blocked); $i++)
     {
         if(self::$_blocked[$i] === str_replace("www.","",$location))
         {
             return true;
         }

             return false;
     }
}

Psuedo code.

NVM. Seem you said you can do expression

Maybe use

parse_url

http://php.net/manual/en/function.parse-url.php

Upvotes: 0

Related Questions