To find and replace text in a PHP string, use strpos() or str_contains() to locate the text and str_replace() to change it. The functions strpos() and str_replace() are the core pair: strpos() returns the position of the first match or false, and str_replace() returns a new string with every match swapped for the replacement.
This guide shows how to search, how to avoid the classic strpos zero-position bug, and how to replace one or many values, with and without case sensitivity. Examples run on PHP 8.x.
strpos($haystack, $needle, $offset = 0) returns the zero-based position of the first occurrence, or false if the text is not found. strrpos() finds the last occurrence and stripos() ignores case.
The offset argument lets you continue searching after a previous match, and a negative offset starts counting from the end of the string.
When the match is at the very start, strpos() returns 0, and 0 is loosely equal to false. A plain if (strpos(...)) therefore fails exactly when the text is at the beginning.
Use !== false with strpos(), or use str_contains() when you only need a yes or no answer. Keep strpos() for cases where you need the position, for example to cut the string at that point with substr().
str_replace($search, $replace, $subject, &$count) replaces every occurrence and is case-sensitive. The optional fourth argument receives the number of replacements.
Notice that Cats with a capital C was left alone, because the search is case-sensitive. The original variable $s is unchanged; the function returns a new string.
Use str_ireplace() to ignore case. Both functions accept arrays, which lets you run several replacements in one call.
When you pass arrays, the replacements run in order, so an earlier replacement can be changed again by a later one. If that is a problem, use strtr($string, ['from' => 'to']), which replaces all pairs in a single pass and prefers the longest match first.
Sometimes you know the position rather than the text. substr_replace() swaps a slice by offset and length, and substr_count() tells you how often a text occurs.
For patterns rather than fixed text, such as "any run of digits", use preg_replace(), which a related article on regular expressions covers. Fixed-text functions are faster and simpler, so choose them whenever a literal match is enough.
| Task | Function |
|---|---|
| Does it contain the text? | str_contains() |
| Where is the first match? | strpos() / stripos() |
| Where is the last match? | strrpos() |
| Replace all matches | str_replace() |
| Replace ignoring case | str_ireplace() |
| Many pairs in one pass | strtr() |
| Replace by position | substr_replace() |
| Count matches | substr_count() |
Position 0 means the text is at the start, but 0 evaluates as false in a loose check. Compare with !== false or use str_contains().
Yes. Use str_ireplace() for a case-insensitive version, or preg_replace() with the i flag for patterns.
str_replace() has no limit argument. Find the position with strpos() and use substr_replace(), or use preg_replace($pattern, $replacement, $subject, 1).
str_replace() works on UTF-8 because it matches whole byte sequences. Positions returned by strpos() are byte offsets, not character offsets, so use mb_strpos() when you need character positions.
Use str_contains() for a yes or no, strpos() with !== false when you need a position, and str_replace() or str_ireplace() to swap text. Pass arrays for many replacements, use strtr() when order matters, and use substr_replace() when you work by offset. Switch to preg_replace() only when a fixed string is not enough.