1

I need to check if a string contains a particular words.

My code is the following:

// Get index.html source
$html = file_get_contents('extract/index.html');

// Bad words checker
$badWords = array("iframe", "alert");
$matches = array();
$matchFound = preg_match_all("/\b(" . implode($badWords,"|") . ")\b/i", $html, $matches);

if ($matchFound) {
    $words = array_unique($matches[0]);
    foreach($words as $word) {
        $results[] = array('Error' => "Keyword found : ". $word);
    }
}
else {
    $results[] = array('Success' => "No keywords found.");
}

Each time I want to execute this, I have the following warning:

Warning: preg_match_all(): Unknown modifier 'w' in /home/public_html/upload.php on line 131

Line 131:

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

Do you know why ?

Thanks.

1 Answers1

1

If one of the bad words is '/w', it can cause this problem. The example below demonstrates this:

$html = 'foobar';

// Bad words checker
$badWords = array("iframe", "alert", '/w');
$matches = array();
$matchFound = preg_match_all("/\b(" . implode($badWords,"|") . ")\b/i", $html, $matches);

Variations of '/w' such as 'foo/wbar' or '/wfoo' would also cause this issue. Go through the badWords and remove the problematic ones.

Edit: Another solution is to use a different delimiter like #. Like this:

$matchFound = preg_match_all("#\b(" . implode($badWords,"|") . ")\b#i", $html, $matches);
Nadine
  • 1,620
  • 2
  • 15
  • 27