How to check if a string does not contain a specific phrase?

How to check if a string does not contain a specific phrase?

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

Stefan Bogdanescu

Founder & Senior Architect

Inverting strpos: Checking for Absence in PHP Strings

Developers frequently need to verify whether a substring exists within a larger string. The standard approach in PHP relies on the strpos function. When this function locates the substring, it returns the starting position as an integer; when it fails to find the text, it returns false. A common pattern to check for presence is using a strict inequality check:

if (strpos($a, 'are') !== false) {
    echo 'true';
}

This snippet explicitly prints 'true' only if the search string is found. However, the logical inverse—printing 'true' if the substring is not found—is often misunderstood or implemented incorrectly due to how strpos handles its return values.

The Logic of Inversion

To invert the function and detect absence, you must simply negate the condition used to detect presence. If the original check uses !== false, the inverted check requires checking if the result is strictly equal to false.

Here is the correct implementation for detecting absence:

if (strpos($a, 'are') === false) {
    echo 'true'; // Substring 'are' was not found in $a
}

It is crucial to use the loose equality operator (==) or strict equality (===) depending on whether you need to distinguish between a boolean false and the integer 0. While strpos returns false when the search fails, using == false works because false converts to 0. However, best practices in PHP generally favor strict typing. Using !== false confirms presence; therefore, === false confirms absence.

Handling Case Sensitivity

A significant pitfall in string searching involves case sensitivity. The standard strpos function is case-sensitive. Searching for 'Are' in a string containing 'are' will return false, even if the letters are present but capitalized differently. If your application logic requires ignoring case, you must pass the search term to strtolower (or strtoupper) before calling strpos.

$searchTerm = 'are';
$textToSearch = 'The Are of the problem is complex';

if (strpos(strtolower($textToSearch), strtolower($searchTerm)) === false) {
    echo 'true'; // Case-insensitive absence check
}

Failing to normalize case can lead to bugs where a substring technically exists in the text but is reported as missing, causing downstream logic errors.

Regex Alternatives for Complex Patterns

While strpos is ideal for literal string matching, it lacks support for regular expressions. If you need to check for the absence of a specific regex pattern (e.g., ensuring a string does not contain an email address or a specific format), you should use preg_match. The function returns 1 on success and false on failure.

To invert this logic for absence, you check that it did not return 1:

if (!preg_match('/pattern/', $text)) {
    echo 'true'; // Pattern was not found
}

Note the use of the negation operator !. This approach is more verbose than strpos but necessary when validation rules extend beyond simple substring containment.

Performance Considerations

For large-scale string operations, performance matters. The strpos function scans the string linearly until it finds a match or reaches the end. If you are checking for absence in a massive buffer, strpos is generally efficient enough for single-pass checks. However, if you need to exclude multiple substrings simultaneously, chaining strpos calls becomes inefficient.

In such scenarios, using str_replace with an empty string to simulate removal and comparing lengths is another technique, though often less readable:

if (strlen($text) === strlen(str_replace('are', '', $text))) {
    echo 'true'; // 'are' was not found
}

This method works because if the replacement actually removed anything, the string length would decrease. If the length remains unchanged, the substring was absent. While valid, this approach modifies the original string variable unless you assign it to a temporary copy, which adds overhead and potential side effects. Stick to strpos for single checks; only consider regex or replacement-based workarounds when specific pattern complexity or batch processing demands dictate otherwise.

Our network