Check out preg_replace here, this is what you are lookin for.

// From the documentation.
preg_replace($regularExpression, $replacement, $subject); 
Answer from Wesley van Opdorp on Stack Overflow
🌐
PHP
php.net › manual › en › function.str-replace.php
PHP: str_replace - Manual
Replace all occurrences of the search string with the replacement string
🌐
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 »
🌐
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.
🌐
Beamtic
beamtic.com › string-replacement-php
How to replace parts of a string with str_replace and preg_replace
November 8, 2020 - The native str_replace function of PHP is used to replace all the occurrences of a given string with a replacement string; but using a regular expression will allow for more complex pattern-based replacements, which is useful for working with ...
🌐
PHP Tutorial
phptutorial.net › home › php tutorial › php str_replace
PHP str_replace - PHP Tutorial
April 7, 2025 - Summary: in this tutorial, you’ll learn how to use the PHP str_replace() function to replace all occurrences of a substring with a new string
🌐
Hacking with PHP
hackingwithphp.com › 4 › 8 › 5 › regular-expression-replacements
Regular expression replacements: preg_replace() – Hacking with PHP - Practical PHP
Note that it is essential to put the $0 inside double quotes so that it is treated as a string - without the quotes, it will just read strtoupper(foo), which is probably not what you meant. ... Optionally you can also pass a fourth parameter to preg_replace() to specify the maximum number of replacements you want to make. For example: <?php $a = "Foo moo boo tool foo"; $b = preg_replace("/[A-Za-z]oo\b/e", 'strtoupper("$0")', $a, 2); print $b; ?>
🌐
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.
🌐
Team Treehouse
teamtreehouse.com › community › strreplace-wildcard-or-regexp
str_replace wildcard or RegExp (Example) | Treehouse Community
You want to use the function preg_replace(). This would be my first crack at it: <?php $string = '/path/to/folder/anything.php'; $pattern = '/\/[^\/]+\.php/'; $replacement = '/replacement.php'; echo preg_replace($pattern, $replacement, $string); ?> Here's what I'm trying to accomplish (but regular expressions always need a bit of trial and error): The $pattern says (at least I tried to make it say) "find every occurrence of a slash followed by one or more characters that are not a slash followed by a dot-p-h-p." The $replacement says "replace any occurrence of that pattern with the following: slash-r-e-p-l-a-c-e-m-e-n-t-dot-p-h-p." Does that help at all?
Find elsewhere
🌐
Lsu
ld2011.scusa.lsu.edu › php › function.str-replace.html
Replace all occurrences of the search string with the replacement string
If search and replace are arrays, then str_replace() takes a value from each array and uses them to do search and replace on subject. If replace has fewer values than search, then an empty string is used for the rest of replacement values. If search is an array and replace is a string, then ...
🌐
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
w3schools.com › php › func_regex_preg_replace.asp
PHP preg_replace() Function
❮ PHP RegExp Reference · Use ... » · The preg_replace() function returns a string or array of strings where all matches of a pattern or list of patterns found in the input are replaced with substrings....
🌐
Envato Tuts+
code.tutsplus.com › home › coding fundamentals › regular expressions
Search and Replace Strings With Regular Expressions in PHP | Envato Tuts+
March 30, 2021 - In this tutorial, we learned how to use some common and useful functions related to regular expressions in PHP. We began by learning how to use preg_match() or preg_match_all() to get all the strings that match a specific pattern. After that, we learned how to use preg_replace() and preg_replace_callback() to replace the matched patterns with some other string.
🌐
Webcodedoc
webcodedoc.com › php › str_replace
PHP str_replace() Function – Syntax, Examples & Common Mistakes
Quick summary: The PHP str_replace() function replaces all occurrences of a search string with a replacement string.
🌐
Stack Overflow
stackoverflow.com › questions › 68326074 › php-str-replace-and-regex
PHP str_replace and regex - Stack Overflow
July 10, 2021 - str_replace ('/ page / * /', '', $ string); Thank you! PHP Collective · php · regex · str-replace · Share · Short permalink to this question · Improve this question · Follow · Follow this question to receive notifications · edited Jul ...
🌐
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 ] )