Check if string contains word in array

Check if string contains word in array

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Handling Partial Matches in Profanity Filters

Your current implementation using strpos() suffers from a fundamental flaw: it performs substring matching rather than whole-word matching. When you search for 'truck' inside the string "This dude is a mothertrucker", strpos() correctly identifies the sequence of characters t-r-u-c-k. However, because it does not verify word boundaries, it triggers a positive match even though the user did not type the forbidden word truck; they typed mothertrucker.

This behavior explains why your logic feels contradictory. If you interpret a match as "say NO!", your code flags mothertrucker as containing the bad word truck. The confusion about running twice likely stems from how you are interpreting the boolean result or iterating through multiple variations of the same root word in your test data, but the core issue is strictly the lack of boundary detection.

To resolve this, you must ensure that the search term exists as a distinct token within the input string. In PHP, this requires checking for whitespace or string delimiters immediately before and after the potential match. A robust approach involves using regular expressions to enforce these boundaries.

You can construct a pattern that looks for the bad word surrounded by non-word characters. Here is how you might structure the logic:

$badWords = ['truck', 'shot'];
$string = "This dude is a mothertrucker";

// Add start and end anchors with word boundaries (\b)
$pattern = '/\b(' . implode('|', $badWords) . ')\b/i';

if (preg_match($pattern, $string)) {
    // Contains a bad word
} else {
    // Clean string
}

The \b token in regular expressions matches the position between a word character (letters, digits, underscore) and a non-word character. In mothertrucker, the sequence truck is followed by er. Since r is a word character, there is no boundary after k. Therefore, \btruck\b will fail to match inside mothertrucker, effectively solving your false positive issue.

While regular expressions are powerful, they introduce complexity and performance overhead if you have thousands of bad words or need to process high-volume chat logs in real-time. For large lists, compiling the regex once outside the loop is crucial, but maintaining the list itself becomes a management task. If you switch frameworks like Laravel, you can leverage built-in helpers that often abstract this logic, though understanding the underlying mechanism remains vital for debugging edge cases.

Another consideration is case sensitivity and normalization. Users might type TRUCK, Truck, or truck. You must decide whether your filter should be strict (case-sensitive) or forgiving (case-insensitive). The example above uses the /i flag to ignore case, ensuring that "MotherTrucker" is also caught if mothertrucker is in your list. However, this adds a slight performance cost compared to a purely case-sensitive check.

If you find yourself dealing with extremely long lists of bad words or needing frequent updates, consider using a finite state machine (like the Aho-Corasick algorithm) or a specialized library such as php-filter or a precompiled trie structure. These approaches allow for constant-time lookups regardless of the input length, which is essential for maintaining low latency in chat applications.

For most standard web applications, however, a well-constructed regular expression using word boundaries offers the best balance between readability and performance. It keeps your code concise while adhering to strict semantic matching rules that prevent accidental flagging of innocent compound words. Always remember to escape special regex characters if your bad words list contains symbols like . or *, though typically profanity lists consist of alphabetic strings.

Our network