0

i have this:

$badWords = array("ban","bad","user","pass","stack","name","html");
$string = 'http://rpmrush.com/uploads/bangalleries/51/original/5e17a20aafe8799843305b7663e6683eda7b3eae.jpg';

$matches = array();
$matchFound = preg_match_all(
                "/\b(" . implode($badWords,"|") . ")\b/i", 
                $string, 
                $matches
              );

if ($matchFound) {
    $return['bannedURL'] = true;
    echo json_encode($return);
    die();
}

Which works fine when the string is simply one of the banned words.

On my website i have a feature for users to enter image urls and they get added, however to stop the users putting illegal etc images (porn and the like) and also using my server as a host for their dodgey images i want to create a list of adult websites and such and then check the input URL against it.

The above code only checks if the string is an exact match, how can i change this to make it work if part of the string matches?

Lovelock
  • 7,689
  • 19
  • 86
  • 186

2 Answers2

0

strpos() would be what you're looking for.

Read more here, question #6284553 (Using an array as needles in strpos). Pretty sure the accepted answer will be of use.

Community
  • 1
  • 1
Mave
  • 2,413
  • 3
  • 28
  • 54
0

I do not see directly why your code wouldn't work. I guess it does. This does work:

$badWords = array("ban","bad","user","pass","stack","name","html");
$string = 'http://rpmrush.com/uploads/ban/galleries/51/original/5e17a20aafe8799843305b7663e6683eda7b3eae.jpg';

$matchFound = preg_match(
                "/\b(" . implode($badWords,"|") . ")\b/i", 
                $string, 
                $matches
              );

if ($matchFound) {
    echo "YOU ARE BAD! --> " . $matches[0] ;
}
else {
    echo "You're good to go!";
}

What might be the problem is the \b : if you leave out the \b's you get a match also with bangalleries against ban.

Roemer
  • 1,124
  • 8
  • 23