Introduction: The Question That Haunts Every PHP Developer
\n\nImagine this: you're writing PHP, and you need to check if a string contains a specific word. It seems simple enough, right? However, when you look on Stack Overflow, you see this question has thousands of views and dozens of answers. People get confused with strpos, use preg_match when a simple function would do, and fall into traps with case sensitivity and encodings. Today, we won't just give you a ready-made answer — we'll look under the hood of PHP and break down how these functions actually work. Ready? Let's go!
The Classic Way: strpos and Its Pitfalls
\n\nThe most popular answer on Stack Overflow is to use strpos. This is a built-in PHP function that finds the position of the first occurrence of a substring. But there's a catch: beginners often write it like this:
$string = "Hello, world!";\n$word = "world";\nif (strpos($string, $word)) {\n echo "Word found!";\n} else {\n echo "Word not found!";\n}\n\n\nAnd this works... until the word is at the beginning of the string. Why? Because strpos returns a position (a number), and if the word is at position 0, then in PHP, 0 is interpreted as false in an if condition. The correct way:
if (strpos($string, $word) !== false) {\n echo "Word found!";\n}\n\n\nHow Does strpos Work Under the Hood?
\n\nWhen you call strpos, PHP internally uses a substring search algorithm. In modern versions of PHP (7.x and 8.x), an optimized algorithm based on the Boyer-Moore-Horspool algorithm is used. It doesn't just iterate through characters one by one; it uses a "bad character" table to skip entire blocks of text if a character from the search word doesn't match. This makes the search very fast, especially for long strings.
But there is a limitation: strpos is case-sensitive. "World" and "world" are different words for it. If you need a case-insensitive check, use stripos — its counterpart that ignores case.
The Modern Way: str_contains (PHP 8.0+)
\n\nStarting with PHP 8.0, the long-awaited str_contains function appeared. It solves the strpos problem once and for all. Instead of checking the position, it simply returns true or false:
$string = "Hello, world!";\n$word = "world";\nif (str_contains($string, $word)) {\n echo "Word found!";\n}\n\n\nWhat's Inside str_contains?
\n\nUnder the hood, str_contains is a wrapper around strpos. It calls the same internal search function, but the result is automatically converted to a boolean value. This makes the code cleaner and safer. However, if you're working with older versions of PHP (7.4 and below), str_contains is not available — you'll have to use strpos with the !== false check.
A Powerful Tool: Regular Expressions with preg_match
\n\nSometimes you need to check not just for a word, but for a whole group of words or a pattern. This is where preg_match comes in. Regular expressions give you superpowers: you can search for whole words (so "world" doesn't match "worldwide"), ignore case, search by word boundaries, and much more.
$string = "Hello, worldwide!";\n$word = "world";\nif (preg_match("/\\b" . preg_quote($word, '/') . "\\b/ui", $string)) {\n echo "Word found!";\n} else {\n echo "Word not found!";\n}\n\n\nHere, \\b is a word boundary, and the u flag enables Unicode support (so Russian letters work correctly). The i flag makes the search case-insensitive. The preg_quote function escapes special characters, if