2

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
1
  • @RiggsFolly sorry, typo Commented Feb 1, 2023 at 13:53

4 Answers 4

2

preg_replace with regex.

$string = ' https://example.com, https://www.example.com, https://subdomain.example.com ';
$olddomain= "example.com";
$newdomain = "stackoverflow.com";

$output = preg_replace('#(https://(www\.)?)' . $olddomain. '#', '$1' . $newdomain, $string);

echo $output;

Output :

https://stackoverflow.com, https://www.stackoverflow.com, https://subdomain.example.com 
Sign up to request clarification or add additional context in comments.

Comments

1

Without using Regex

$string = ' https://example.com, https://www.example.com, https://subdomain.example.com ';
$olddomain= "example.com";
$newdomain = "stackoverflow.com";

$parts = explode(",", $string);
$new_parts = [];

foreach ($parts as $part) {
    $new_parts[] = str_replace(['https://', 'http://', $olddomain], ['https://', 'http://', $newdomain], $part);
}

$output = implode(",", $new_parts);

echo $output;

Return

https://stackoverflow.com, https://www.stackoverflow.com, https://subdomain.example.com 

Comments

1

With Array and Regex

$string = ' https://example.com, https://www.example.com, https://subdomain.example.com ';
$olddomain = ["example.com", 'www\.'];
$newdomain = ["stackoverflow.com", ''];

$output = preg_replace('#https://(' . implode('|', $olddomain) . ')#', 'https://' . $newdomain[0], $string);
$output = preg_replace('#(' . $olddomain[1] . ')#', $newdomain[1], $output);

echo $output;

Comments

0

No need for a regex, just pass the www. with the other change in into str_replace(). Remember str_replace() accepts an array of things to "change from" and "change to".

$string = ' https://example.com, https://www.example.com, https://subdomain.example.com ';
$olddomain = ["example.com",'www.'];
$newdomain = ["stackoverflow.com", ''];

$output = str_replace($olddomain, $newdomain, $string);
echo $output;

RESULT

 https://stackoverflow.com, https://stackoverflow.com, https://subdomain.stackoverflow.com 

5 Comments

https://stackoverflow.com.stackoverflow.com have you noticed this error. it should be https://stackoverflow.com not https://stackoverflow.com.stackoverflow.com
FIxed it already, sorry someone asked me a question and I got distracted
No problem. yes, it working. But how it working. ["example.com",'www.']; means it will try to match in both combination?
No, it does the changes one occurance at a time So look for $olddomain[0] if found change to $newdomain[0] etc etc
So, it work as for loop with PHP Array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.