drjgk44268 2016-12-18 04:09
浏览 34
已采纳

在PHP中,如何保留链接的第一部分,保持第二部分并动态更改其余部分

Foe example. Lets take 4 URLs (existing or future) that follow the targeted pattern of:
https://KeepThisPartTill-cde.com/a/b/cde/ThisWillBeDifferent/ThisWillNot
https://KeepThisPartTill-cde.com/a/b/cde/ThisIsDifferent/ThisWillNot
https://KeepThisPartTill-cde.com/a/b/cde/ChangedAgain/ThisWillNot
https://KeepThisPartTill-cde.com/a/b/cde/AndAgain/ThisWillNot

So. If any of these (existing or future) links is clicked, we need them to be changed to:
https://KeepThisPartTill-cde.com/a/b/cde/WhateverThisWillBe/ThisWillNot?SomethingHere

This means:
If one clicks on a link that starts with https://KeepThisPartTill-cde.com/a/b/cde/, keep this part, add the part WhateverThisWillBe (which extends until you find the next /), add /ThisWillNot? (notice the ?) and finally add SomethingHere

Should I probably be using .htaccess

Please keep in mind that although it is about a WordPress site, I can figure the SomethingHere which is WordPress related but have a difficult time figuring out how to keep the first part intact when the second WhateverThisWillBe part is different (in every single link) and add /ThisWillNot? after it.

  • 写回答

1条回答 默认 最新

  • dsjbest2015 2017-01-24 10:12
    关注

    Using preg_replace:

    preg_replace('~(https://KeepThisPartTill-cde.com/a/b/cde/).+?(/ThisWillNot)~i',
                 "$1WhateverThisWillBe$2?",
                  $string);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答