PHP string "contains"

PHP string "contains"

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

Stefan Bogdanescu

Founder & Senior Architect

PHP String "Contains" Performance and Patterns

Checking if a string contains a specific character, such as a dot (.), is a fundamental operation in PHP development. While the language offers several mechanisms to perform this check, choosing the right one depends on the specific constraints of your application, including performance requirements, readability, and regex complexity.

The most straightforward approach utilizes the str_contains() function, introduced in PHP 8.0. This built-in utility is optimized for simple substring checks. It returns a boolean true if the needle is found within the haystack and false otherwise. Before this version existed, developers relied on the deprecated strpos() function combined with a truthiness check:

if (strpos($string, '.') !== false) {
    // The string contains a dot
}

While strpos() works in all PHP versions, relying on it for new codebases can introduce compatibility issues with older environments. Furthermore, the syntax requires an extra comparison operator (!== false), which adds cognitive load compared to the direct return value of str_contains(). If you are maintaining a legacy project running PHP 7.x or earlier, sticking with strpos() is mandatory; otherwise, str_contains() offers cleaner, more readable code.

For scenarios involving complex patterns rather than a single character, regular expressions become necessary. The preg_match() function allows you to search for specific patterns defined by regex syntax. To check for a dot using regex:

if (preg_match('/\./', $string)) {
    // Pattern found
}

It is critical to note the escaping of the dot in the pattern. In regular expressions, the dot is a special character representing "any character." Therefore, /\./ must be used to match a literal period. A common pitfall here is omitting the escape slash, which would result in matching any character instead of specifically looking for a dot.

While regex is powerful, it incurs a higher computational cost than simple string functions because the engine must parse the pattern and build a state machine before executing the search. If you are simply checking for a literal dot or other single characters, using regex adds unnecessary overhead. The str_contains() function operates in C with highly optimized linear scans, making it significantly faster for this specific use case.

There is also the option of looping through the string character by character. While technically possible, this approach is almost always inferior to built-in functions. A manual loop forces the interpreter to handle iteration logic in PHP rather than leveraging optimized native code paths. This results in slower execution times and more verbose code that is harder to maintain.

In high-frequency contexts where a string might be checked repeatedly—such as validating file extensions or parsing URLs—using str_contains() provides the best balance of speed and clarity. However, if your logic requires you to extract the dot or manipulate the string immediately after finding it, strpos() remains relevant because it returns the position index, whereas str_contains() only returns a boolean.

For developers working within the Laravel ecosystem, these native PHP functions integrate seamlessly. You can utilize them directly in controllers, middleware, or service classes without needing additional dependencies. If you are dealing with complex validation rules where regex is unavoidable, ensure your patterns are compiled once and reused to minimize the cost of parsing, though for a single character like ., this optimization is rarely needed.

Our network