How to check if a string contains a specific text

How to check if a string contains a specific text

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

Stefan Bogdanescu

Founder & Senior Architect

How to Check if a String Contains a Specific Text

In PHP, determining whether a variable contains a specific substring is a fundamental operation that appears frequently in form validation, log analysis, and string manipulation tasks. The syntax you provided, $a exist 'some text', contains two distinct errors: the operator exist is not valid PHP syntax, and the logic for checking substring presence requires a specific function.

The correct approach depends entirely on whether you are looking for exact equality or if you simply need to know if the substring exists anywhere within the string. For substring detection, the standard solution is the strpos() function. This function returns the starting position of the first occurrence of a needle inside a haystack, or false if the needle is not found.

Using strpos() for Substring Detection

To check if $a contains 'some text', you must wrap the strpos() call in a boolean context. Because strpos() returns an integer index (e.g., 0, 5, 12) on success and false on failure, a loose comparison (==) can lead to unexpected results. If your target string is "some text", strpos() returns 0. In PHP, the integer 0 evaluates to false in a boolean context, meaning a simple if ($a == 'some text') would incorrectly treat an empty match at the start as a failure if you aren't careful with type juggling.

The robust pattern is:

$a = '';

if (strpos($a, 'some text') !== false) {
    echo 'text';
}

This strict comparison (!==) ensures that a return value of 0 is treated as a valid match, while false triggers the condition failure. This distinction is critical when validating user input or parsing logs where the substring might appear right at the beginning of the string.

Case Sensitivity and Normalization

A common pitfall with strpos() is that it is case-sensitive. Searching for 'Some Text' in a string containing "some text" will yield false. If your application needs to ignore case variations, you must normalize the strings before searching. You can use stripos() instead, or explicitly convert both strings to lowercase using strtolower():

if (strpos(strtolower($a), strtolower('some text')) !== false) {
    echo 'text';
}

Alternatively, stripos() handles this internally:

if (stripos($a, 'some text') !== false) {
    echo 'text';
}

Using stripos() is generally preferred for readability and maintainability when case-insensitivity is required. It reduces the cognitive load of remembering whether your data might contain mixed-case variations.

Handling Binary Data and Special Characters

When dealing with binary data or strings containing null bytes, standard string functions can behave unexpectedly because PHP treats strings as arrays of characters in some contexts. If $a might contain binary content, consider using mb_strpos() if you are working with UTF-8 encoded strings to ensure proper byte-order handling, especially if the needle contains multi-byte characters.

Alternative: The String Contains Operator

In modern PHP versions (7.4+), a more concise alternative exists using the spaceship operator combined with length checks, but strictly speaking, strpos() remains the most performant and clear method for substring existence checks in legacy and modern codebases alike.

If you are building complex validation logic, wrapping this check in a helper method or trait can prevent repetition across your application. However, keep the implementation simple: use strpos() with strict inequality checking to ensure accuracy regardless of where the substring appears.

Our network