🌐
PHP
php.net › manual › en › function.str-replace.php
PHP: str_replace - Manual
This function returns a string or an array with the replaced values. ... <?php // Provides: <body text='black'> $bodytag = str_replace("%body%", "black", "<body text='%body%'>"); echo $bodytag, PHP_EOL; // Provides: Hll Wrld f PHP $vowels = ...
🌐
W3Schools
w3schools.com › php › func_string_str_replace.asp
PHP str_replace() Function
If find is an array and replace is a string, the replace string will be used for every find value · Note: This function is case-sensitive. Use the str_ireplace() function to perform a case-insensitive search. Note: This function is binary-safe. ... <?php $arr = array("blue","red","green","yellow"); print_r(str_replace("red","pink",$arr,$i)); echo "Replacements: $i"; ?> Try it Yourself »
🌐
Benjamin Crozat
benjamincrozat.com › home › blog › php str_replace(): examples, gotchas, and alternatives
PHP str_replace(): examples, gotchas, and alternatives
1 month ago - $message = 'Hello, unknown person!'; echo str_replace('unknown person', 'Benjamin', $message); // Hello, Benjamin! PHP replaces all matching occurrences, not just the first one.
🌐
Udemy
blog.udemy.com › home › it & development › web development › how to use the php str_replace function to find and replace strings
How to Use the PHP STR_REPLACE Function - Udemy Blog
April 14, 2026 - The PHP `str_replace()` function searches a string or array and replaces every matching instance with a new value. This article covers its syntax, parameters, case sensitivity, empty-string replacements, and array usage.
🌐
W3Schools
www-db.deis.unibo.it › courses › TW › DOCS › w3schools › php › func_string_str_replace.asp.html
PHP str_replace() Function
If find is an array and replace is a string, the replace string will be used for every find value · Note: This function is case-sensitive. Use the str_ireplace() function to perform a case-insensitive search. Note: This function is binary-safe. ... <?php $arr = array("blue","red","green","yellow"); print_r(str_replace("red","pink",$arr,$i)); echo "Replacements: $i"; ?> Run example »
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-str_replace-function
PHP str_replace() Function - GeeksforGeeks
November 30, 2021 - The str_replace() is a built-in function in PHP and is used to replace all the occurrences of the search string or array of search strings by replacement string or array of replacement strings in the given string or array respectively.
🌐
ReqBin
reqbin.com › code › php › zcmyga8t › php-string-replace-example
How do I replace a string in PHP?
January 14, 2023 - To replace a string with a substring in PHP, use the str_replace($search, $replace, $subject, $count) function.
🌐
PHP Tutorial
phptutorial.net › home › php tutorial › php str_replace
PHP str_replace - PHP Tutorial
April 7, 2025 - The PHP str_replace() function returns a new string with all occurrences of a substring replaced with another string.
🌐
Phpmentoring
phpmentoring.org › blog › php-str-replace
PHP Str_Replace() Function - How To Replace Characters In A String In PHP
The PHP str_replace() function is a built-in text processing function that is used to replace all the occurrences of a given search string or array with a replacement string or array in a given string or array.
Find elsewhere
🌐
Functions-Online
functions-online.com › str_replace.html
test str_replace online - PHP string functions - functions-online
If you don't need fancy replacing rules (like regular expressions), you should always use this function instead of ereg_replace() or preg_replace(). string str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
🌐
Codecademy
codecademy.com › docs › php › string functions › str_replace()
PHP | String Functions | str_replace() | Codecademy
July 28, 2023 - The str_replace() function returns a string with occurrences of a specified substring replaced by another string.
🌐
Reintech
reintech.ai › blog › comprehensive-guide-php-str-replace-function
A Comprehensive Guide to PHP's `str_replace()` Function
April 14, 2023 - String manipulation is fundamental to PHP development, and str_replace() stands out as one of the most practical functions in the language. Unlike complex regular expressions or manual parsing, this function offers a straightforward approach to finding and replacing text within strings.
🌐
Pi My Life Up
pimylifeup.com › home › how to use the str_replace function in php
How to use the str_replace Function in PHP - Pi My Life Up
April 17, 2022 - It can return either your modified PHP string, or an array of modified strings depending what is passed into the “$search” parameter. str_replace( array|string $search, array|string $replace, array|string $subject, int &count = null ): string|arrayCopy
🌐
Dev Lateral
devlateral.com › home › guides › php › the complete php str_replace tutorial
The Complete PHP str_replace Tutorial | Dev Lateral
July 24, 2023 - PHP str_replace is a great yet powerful tool for all those wanting to take a string and replace some of its contents. As a native PHP function, you'll benefit from the inherited performance benefit as opposed to writing you're own variant of this function. If you're looking to replace parts of a string in your PHP code then str_replace (short for string replace) is the function for you.
🌐
Medium
inyomanjyotisa.medium.com › mastering-phps-str-replace-function-59fb800d4ac9
Mastering PHP’s str_replace() Function | by I Nyoman Jyotisa | Medium
December 18, 2024 - This function is essential for string manipulation and is widely used in various PHP applications to handle text efficiently. The str_replace() function in PHP is designed to replace specific substrings within a string.
🌐
Scaler
scaler.com › home › topics › php str_replace() function
PHP str_replace() Function - Scaler Topics
March 18, 2024 - str_replace is a versatile string manipulation function in PHP used to replace occurrences of a specified substring with another in a given string.
🌐
OnlinePHP
onlinephp.io › str-replace › manual
str_replace - OnlinePHP.io Example
If search and replace are arrays, then str_replace takes a value from each array and uses them to search and replace on subject. If replace has fewer values than search, then an empty string is used for the rest of replacement values.
Top answer
1 of 16
802

There's no version of it, but the solution isn't hacky at all.

$pos = strpos($haystack, $needle);
if ($pos !== false) {
    $newstring = substr_replace($haystack, $replace, $pos, strlen($needle));
}

Pretty easy, and saves the performance penalty of regular expressions.

Bonus: If you want to replace last occurrence, just use strrpos in place of strpos.

Here it is as a reusable function:

/**
 * Like str_replace, but only replaces once.
 * 
 * Example:
 * <code>
 * // $replaced: 'I have 50 dogs and 100 cats'
 * $replaced = str_replace_first_instance('I have 100 dogs and 100 cats', '100', '50');
 * </code>
 */
function str_replace_first_instance(string $haystack, string $needle, string $replacement_text): string
{
    $needle_position = strpos($haystack, $needle);
    
    if ($needle_position !== false)
    {
        $haystack = substr_replace($haystack, $replacement_text, $needle_position, strlen($needle));
    }
    
    return $haystack;
}

Example usage:

//This will return 'I have 50 dogs and 100 cats'
str_replace_first_instance('I have 100 dogs and 100 cats', '100', '50')
2 of 16
414

Can be done with preg_replace:

function str_replace_first($search, $replace, $subject)
{
    $search = '/'.preg_quote($search, '/').'/';
    return preg_replace($search, $replace, $subject, limit: 1);
}

echo str_replace_first('abc', '123', 'abcdef abcdef abcdef'); 
// outputs '123def abcdef abcdef'

The magic is in the optional fourth parameter, [Limit]. From the documentation:

[Limit] - The maximum possible replacements for each pattern in each subject string. Defaults to -1 (no limit).


Though, see zombat's answer for a more efficient method (roughly, 3-4x faster).