替换PHP变量中的部分URL

I am using PHP to set the current URL as a variable using

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

This would echo the string:

http://www.example.com/landing-page-demo/

I would like to replace the 'landing-page' part of the string with 'confirmation-page' and then save this updated URL as another variable.

I was thinking of using str replace, is this the most ideal method of doing this? Not sure how to approach the problem

Indeed, short of knowing regular expressions, str_replace will do the trick.

Perform str_replace on $_SERVER['REQUEST_URI']

Example:

$url = 'http://' . $_SERVER['SERVER_NAME'] . str_replace("landing", "confirmation", $_SERVER['REQUEST_URI']);

My current solution using Str Replace:

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; // Full URL var
$redirecturl = str_replace("landing","confirmation", $url);

Using str_replace is the easiest solution however you can do your logic with pathinfo($url)

    $url = "http://www.example.com/landing-page-demo/";
    $newURL = str_replace('landing', 'confirmation', $url);
    echo $newURL;