0

I have an array:

$blacklist = array("asdf.com", "fun.com", "url.com");

I have an input string:

$input = "http://asdf.com/asdf/1234/";

I am trying to see if string $input matches any values in $blacklist.

How do I accomplish this?

0

5 Answers 5

3

Sounds like a decent use for parse_url():

<?php
    $blacklist = array("asdf.com", "fun.com", "url.com");
    $input = "http://asdf.com/asdf/1234/";

    $url = parse_url($input);

    echo (in_array($url['host'], $blacklist) ? '(FAIL)' : '(PASS)') . $url ['host'];
?>

Output:

(FAIL)asdf.com 
Sign up to request clarification or add additional context in comments.

Comments

1

Using foreach is probably the best solution for what you're trying to achieve.

$blacklist = array("/asdf\.com/", "/fun\.com/", "/url\.com/");

foreach($blacklist as $bl) {
  if (preg_match($bl, $input)){return true;}
}

3 Comments

what are you return'ing to?
@jnpcl If he's using a function check. Of course it's optional. Pretty obvious...
It may be obvious to those of us offering answers, but it may not be obvious to the OP. Your answer does not work "as is", and though it is a viable solution, it requires extra code that the OP may not realize he needs to add.
1

One way could be (but I didn't measure performance):

$san = preg_replace($blacklist, '', $input);

if($san !== $input) {
    //contained something from the blacklist
}

If the input does not contain any string from the backlist, the string will be returned unchanged.

An other, maybe better suited and definitely more efficient approach could be to extract the host part from the input and create the blacklist as associative array:

$blacklist = array(
      "asdf.com" => true,
      "fun.com" => true, 
      "url.com" => true
);

Then testing would be O(1) with:

if($blacklist[$host]) {
    //contained something from the blacklist
}

4 Comments

Didn't you know how much I hate down votes without a comment? Fear my wrath!
@KingCrunch: Yes the first one is more or less a hack. Lets say I thought about a creative way ;) Regarding the second: Arrays in PHP are ordered maps, they are neither lists nor sets. in_array will always be slower as it has to perform a linear search. This is not relevant for small arrays of course...
Arrays are implemented as hash maps, not only as maps. However, they can be treated differently in different situations. So for example you can use an array as a set, list, stack, queue, or whatever. Its just about naming here and has not much to do with the implemenation. And about in_array(): Clean code > micro optimization
@KingCrunch: I'm just repeating what is written in the documentation (normally hash maps cannot guarantee order, but PHP arrays do). I meant ordered hash maps. Of course they can be used as (nearly) anything else, that's why arrays are so great :) Imo clean code is subjective to some degree. I personally find a lookup map much cleaner as a linear search. But anyway, now I know at least what you didn't like. Thank you!
0

in_array is of no use, as it searches for the exact string.

You have to loop through the array, and search for it

foreach($str in $blacklist)
{
   if( stristr($input, $str ) )
    {
         //found
    }
}

Comments

0

This code should work:

$blacklist = array("asdf.com", "fun.com", "url.com");
$input = "http://asdf.com/asdf/1234/";
if (in_array(parse_url($input,PHP_URL_HOST),$blacklist))
  {
  // The website is in the blacklist.
  }

1 Comment

Which is essentially the same as given in @jnpcl's answer. There is no need to duplicate answers... -1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.