Menu

PHP str_contains(): Check If a String Contains Text

str_contains($haystack, $needle) returns true if $needle appears anywhere in $haystack. Learn the case-insensitive version, str_starts_with and str_ends_with, checking for several words, and the strpos version for PHP 7.

This page includes runnable editors - edit, run, and see output instantly.

str_contains($haystack, $needle) returns true if $needle appears anywhere in $haystack, and false otherwise. It was added in PHP 8.0 and is case-sensitive.

Unlike strpos(), it returns a real boolean, so there is no "found at position 0 looks like false" trap.

Syntax

str_contains(string $haystack, string $needle): bool
str_starts_with(string $haystack, string $needle): bool
str_ends_with(string $haystack, string $needle): bool

All three were added in PHP 8.0 and work byte by byte, so they handle Japanese and other UTF-8 text correctly without an mb_ version.

Check the start or end with str_starts_with and str_ends_with

Case-insensitive contains

There is no str_icontains(). Lowercase both strings first. mb_strtolower() also lowercases letters outside ASCII, such as É or Cyrillic, which strtolower() leaves alone.

The empty needle returns true

An empty needle is found in every string, including an empty one. This matters when the needle comes from a search box that the user left blank.

Check if a string contains any or all of several words

str_contains() takes one needle. Loop over an array of needles: return as soon as one matches for "any", or as soon as one fails for "all".

To check whether a value is one of the elements of an array, as opposed to a substring of a string, use in_array().

Before PHP 8: strpos !== false

Code that has to run on PHP 7 uses strpos() with a strict comparison. It gives the same answer as str_contains(), except for an empty needle: PHP 7's strpos() printed Warning: strpos(): Empty needle and returned false.

The last line prints bool(false) even though the text is there, because php sits at position 0. Replacing such checks with str_contains() is one of the easiest PHP 8 upgrades.

Frequently Asked Questions

How do I check if a string contains a word in PHP?

On PHP 8 use str_contains($haystack, $needle), which returns true or false: str_contains('Hello world', 'world') is true. On PHP 7 use strpos($haystack, $needle) !== false.

Is str_contains case-sensitive?

Yes. For a case-insensitive check, lowercase both sides: str_contains(mb_strtolower($haystack), mb_strtolower($needle)), or use stripos($haystack, $needle) !== false.

Why does str_contains return true for an empty string?

Every string contains the empty string, so str_contains('abc', '') returns true. If the needle comes from user input, check that it is not empty first.

How do I check if a string starts or ends with something in PHP?

Use str_starts_with($s, 'http') and str_ends_with($s, '.pdf'), both added in PHP 8.0. Like str_contains(), they are case-sensitive and return a bool.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED