Laravel 5.5 - Check if string contains exact words

Laravel 5.5 - Check if string contains exact words

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

Stefan Bogdanescu

Founder & Senior Architect

Laravel 5.5 - Check if string contains exact words

In Laravel, you often need to validate user input against a list of restricted terms. A common scenario involves checking a $string against a $blacklistArray to ensure no offensive or prohibited language slips through. However, a naive implementation using str_contains can lead to false positives that compromise your content filters.

Consider this specific case from a typical development query:

$string = 'Cassandra is a clean word so it should pass the check';
$blacklistArray = ['ass', 'ball sack'];

// This approach fails because "Cass" contains "ass"
$contains = str_contains($string, $blacklistArray); 

In Laravel 5.5 and earlier versions, str_contains accepts an array and returns a boolean indicating whether the string contains any of the substrings provided in the array. While convenient for simple substring matching, this behavior is disastrous for word-based filtering. In the example above, the function returns true because it finds "ass" inside "Cassandra". The intent here is clearly to flag only words that exist independently, not partial matches within larger vocabulary.

To solve this, you must shift from substring matching to whole-word matching. The most robust approach involves using a regular expression with word boundaries. In PHP, the preg_match function allows you to define patterns that ensure a match occurs only between non-alphanumeric characters or at the start/end of the string.

Here is how you can refactor the logic for Laravel 5.5:

$string = 'Cassandra is a clean word so it should pass the check';
$blacklistArray = ['ass', 'ball sack'];

// Build a regex pattern with word boundaries (\b)
// \b ensures we match whole words only, not substrings
$searchPattern = '/\b(' . implode('|', array_map(function($word) {
    // Escape special regex characters in the blacklist items
    return preg_quote($word, '/');
}, $blacklistArray)) . ')\b/i';

$contains = preg_match($searchPattern, $string);

if ($contains > 0) {
    echo "Blacklisted content detected.";
} else {
    echo "Content is clean.";
}

This solution constructs a dynamic regular expression by joining the array items with an alternation operator (|). The preg_quote function is essential here; without it, characters like dots or hyphens in blacklist entries would be interpreted as regex metacharacters rather than literal text. The \b token acts as a word boundary, effectively telling the engine to stop matching if it encounters a letter immediately before or after the target word.

One significant trade-off to consider is performance. Building a complex regular expression for every check adds overhead compared to a simple str_contains. If your blacklist grows to hundreds of terms, you might hit regex complexity limits or slow down validation requests. In high-throughput environments, maintaining a static compiled regex or using a dedicated library like php-ble (Banned List Engine) can be more efficient than rebuilding the pattern on every request.

Another pitfall is handling punctuation. The \b boundary treats punctuation as non-word characters, so "word." matches against "word". However, if your blacklist contains terms with internal punctuation or if you need to ignore case strictly, you must adjust the flags. The /i flag in the example ensures case-insensitivity, which is usually desired for content moderation but can sometimes obscure subtle nuances depending on your specific business rules.

If you find yourself needing advanced filtering capabilities beyond basic regex, LaravelCompany offers tools and resources that integrate seamlessly with these patterns to help manage complex text validation workflows.

Our network