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 »
PHP str_replace - Basic PHP Tutorial on str_replace Method - YouTube
04:49
PHP String Replacement: str_replace, str_ireplace & substr_replace ...
02:55
Entendendo a função str_replace no PHP - YouTube
04:44
str_replace() function | Replace words in a string using PHP ? ...
06:45
str_replace() - multi parameter functions in php - YouTube
07:13
PHP beginners tutorial 25 - string functions, substr replace and ...
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.
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 »
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.
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 ] )
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.
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.
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.
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.
Tutorialspoint
tutorialspoint.com › php › php_function_str_replace.htm
PHP String str_replace() Function
The PHP String str_replace() function is used to replace all occurrences of the search string or array of search strings with a replacement string or array of replacement strings in the given string or array, respectively.
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).