duanmao1975
2015-10-05 10:34
浏览 16
已采纳

替换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

图片转代码服务由CSDN问答提供 功能建议

我使用PHP将当前URL设置为变量

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

这将回显字符串:

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

我想用'confirmation-page'替换字符串的'landing-page'部分,然后将这个更新的URL保存为另一个变量。

我在考虑使用str替换 ,这是最理想的做法吗? 不知道如何解决问题

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doushanlv5184 2015-10-05 10:36
    已采纳

    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']);
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douchuifk90315 2015-10-05 10:38

    My current solution using Str Replace:

    $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; // Full URL var
    $redirecturl = str_replace("landing","confirmation", $url);
    
    评论
    解决 无用
    打赏 举报
  • doujie4787 2015-10-05 10:42

    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;
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题