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 OverflowPHP
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.
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; ?>
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?
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 ...
Top answer 1 of 3
1
$string = preg_replace('/src\s*=/i', 'a', $string);
What that RegEx means is "match src followed by 0 or more spaces followed by =".
2 of 3
0
preg_replace just works as str_replace:
$new_string = preg_replace($pattern, $replacement, $old_string);
The pattern in your case would be "@src\s*=@i" (\s* covers zero to infinite whitespaces, @ is just one out of different possible delimeters the pattern requires, i (after the last delimiter) makes the pattern case insensitive.)
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 ...
Top answer 1 of 3
52
$input_lines="any word here related to #English must #be replaced.";
$result = preg_replace("/(#\w+)/", "<a href='bla bla'>$1</a>", $input_lines);
DEMO
OUTPUT:
any word here related to <a href='bla bla'>#English</a> must <a href='bla bla'>#be</a> replaced.
2 of 3
8
This should nudge you in the right direction:
echo preg_replace_callback('/#(\w+)/', function($match) {
return sprintf('<a href="https://www.google.com?q=%s">%s</a>',
urlencode($match[1]),
htmlspecialchars($match[0])
);
}, htmlspecialchars($text));
See also: preg_replace_callback()
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 ] )