I am using preg_replace
or str_replace
in PHP to replace all the domain name including www
$string = ' https://example.com, https://www.example.com, https://subdomain.example.com ';
$olddomain= "example.com";
$newdomain = "stackoverflow.com";
$output = str_replace($olddomain, $newdomain, $string);
$output = preg_replace('#(www[.])?[.]' . $olddomain. '#', $newdomain, $body);
echo $output;
My expectation:
https://example.com -> https://stackoverflow.com
https://www.example.com -> https://stackoverflow.com
https://subdomain.example.com -> https://subdomain.example.com
4
Answers
No need for a regex, just pass the
www.
with the other change in intostr_replace()
. Rememberstr_replace()
accepts an array of things to "change from" and "change to".RESULT
preg_replace with regex.
Output :
Without using
Regex
Return
With Array and Regex