PHP String Search & Replace
In this page:
$position = strpos($haystack, $needle); // false if not found
str_contains($haystack, $needle); // PHP 8+
$result = str_replace($search, $replace, $subject);
Finding Substring Position
strpos() returns the numeric position of the first occurrence of a substring within a string, or false if it isn't found — a common pitfall is using == false instead of === false, since a match at position 0 is falsy under loose comparison.
Example: Finding Substring Position
<?php
// Declare `$text`, set to "Hello World"
$text = "Hello World";
// Declare `$pos`, set to `strpos($text, "World")`
$pos = strpos($text, "World");
// Check whether `$pos !== false`
if ($pos !== false) {
// Print "Found at position $pos" to the output
echo "Found at position $pos";
}
?>
Login to try C/C++/Java/PHP code in the editor
Basic String Replacement
str_contains() (PHP 8+) directly answers whether a substring exists anywhere inside a string as a clean boolean, replacing the older, more error-prone pattern of checking strpos() against false.
Example: Basic String Replacement
<?php
// Check whether `!function_exists('str_contains')`
if (!function_exists('str_contains')) {
// Define the function `str_contains` taking `$haystack`, `$needle`
function str_contains($haystack, $needle) {
// Return `strpos($haystack, $needle) !== false` from this function
return strpos($haystack, $needle) !== false;
}
}
// Declare `$text`, set to "Hello World"
$text = "Hello World";
// Print a detailed dump (with types) of `str_contains($text, "World")`
var_dump(str_contains($text, "World"));
?>
Login to try C/C++/Java/PHP code in the editor
Case-Insensitive Replacement
str_replace() swaps every occurrence of a target substring with a replacement, and accepts arrays for both arguments so you can perform several find-and-replace operations in a single call, such as stripping multiple unwanted characters at once.
Example: Case-Insensitive Replacement
<?php
// Declare `$text`, set to "The Quick Brown Fox"
$text = "The Quick Brown Fox";
// Print `str_replace(["Quick", "Brown"], ["Slow", "Red"], $text)` to the output
echo str_replace(["Quick", "Brown"], ["Slow", "Red"], $text);
?>
Login to try C/C++/Java/PHP code in the editor
Searching Case-Insensitively
substr() extracts a portion of a string starting at a given index for an optional given length, which is how you'd pull out, say, the first 100 characters of a blog post to use as a preview excerpt.
Example: Searching Case-Insensitively
<?php
// Declare `$post`, set to "This is a long blog post about PHP arrays and functions."
$post = "This is a long blog post about PHP arrays and functions.";
// Print `substr($post, 0, 20)` to the output
echo substr($post, 0, 20);
?>
Login to try C/C++/Java/PHP code in the editor
Replacing Portions by Index
str_ireplace() and stripos() are the case-insensitive counterparts of str_replace() and strpos(), useful when you need to find or replace text regardless of whether the user typed it in uppercase, lowercase, or mixed case.
Example: Replacing Portions by Index
<?php
// Declare `$text`, set to "Hello WORLD"
$text = "Hello WORLD";
// Print `str_ireplace("world", "PHP", $text) . "\n"` to the output
echo str_ireplace("world", "PHP", $text) . "\n";
// Print a detailed dump (with types) of `stripos($text, "world")`
var_dump(stripos($text, "world"));
?>
Login to try C/C++/Java/PHP code in the editor
- Testing
strpos($s, a)withif (strpos(...)), which is false when the match is at position 0; compare with!== falseinstead. - Using
str_replaceand expecting it to change the original variable, when it returns a new string. - Using
str_replaceand expecting it to ignore case, when you needstr_ireplacefor case-insensitive replacement.
- Strings support escape characters, string functions, and formatting.
- Heredoc and nowdoc handle longer text, and search and replace change text content.
- Regular expressions match patterns in text.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: