skip to Main Content

I want to replace all the string values from example.com to sample.com excluding the image extension .png, .jpeg, .jpg etc.

$body = ' www.example.com, example.com, example.com/hello.html, example.com/logo.png, example.com/yourbaby.php , example.com/newimage.jpg, www.example.com/newimage.jpg';

//values    
$oldomain = ['example.com','www.']; 
$newdomain= ['sample.com', ''];
$excludeextension = [".png", ".jpg", ".jpeg"];

//Replace    
$body = str_replace($olddomain, $newdomain, $body);
$body = str_replace($excludeextension, '', $body);

//output    
 echo $body; 

Output i am looking for :

sample.com, sample.com, sample.com/hello.html, example.com/logo.png, sample.com/yourbaby.php , example.com/newimage.jpg, www.example.com/newimage.jpg

Expectation

https://example.com -> https://sample.com

https://www.example.com -> https://sample.com

https://subdomain.example.com -> https://subdomain.sample.com

https://www.example.com/image.png -> https://www.example.com/image.png

https://example.com/image.png -> https://example.com/image.png

2

Answers


  1. You can do this using str_replace() and a bit of exploding and imploding.

    $body = ' www.example.com, example.com, example.com/hello.html, example.com/logo.png, example.com/yourbaby.php, example.com/newimage.jpg, www.example.com/newimage.jpg';
    
    $oldomain = ['example.com','www.']; 
    $newdomain= ['sample.com', ''];
    $excludeextension = ["png", "jpg", "jpeg"];
    
    $doms = explode(',', $body);
    foreach ($doms as &$dom) {
        // get extension
        $pi = pathinfo($dom);
        if ( ! in_array( $pi['extension'], $excludeextension) ){
            $dom = str_replace($oldomain, $newdomain, $dom);
        }
    
    }
    $NewStr = implode($doms);
    echo $NewStr;
    
    

    RESULT

    sample.com sample.com sample.com/hello.html example.com/logo.png sample.com/yourbaby.php example.com/newimage.jpg www.example.com/newimage.jpg
    
    Login or Signup to reply.
    1. Using PATHINFO_EXTENSION & str_replace
    2. First explode than implode

    $body = 'example.com, www.example.com, https://www.example.com, https://example.com, subdomain.example.com, example.com/logo.png, www.example.com/logo.png, example.com/hello/logo.png, https://example.com/logo.png, subdomain.example.com/logo.png"';

    //Defined Values 
    $oldomain = ["example.com", "www.example.com"];
    $newdomain = "sample.com";
    $excludeextension = ["png", "jpg", "jpeg", "css"];
    
    // Split the string into an array of URLs
    $urls_body_extension = explode(", ", $body);
    
    // Loop through each URL and replace it if necessary
    foreach ($urls_body_extension as &$url_body_extension) {
        $extension_body_extension = pathinfo($url_body_extension, PATHINFO_EXTENSION);
        if (!in_array($extension_body_extension, $excludeextension)) {
            foreach ($oldomain as $domain_body_extension) {
                if (strpos($url_body_extension, $domain_body_extension) !== false) {
                    $url_body_extension = str_replace($domain_body_extension, $newdomain, $url_body_extension);
                    break;
                }
            }
        }
    }
    
    // Join the modified URLs back into a string
    $body = implode(", ", $urls_body_extension);
    
    echo $body;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search