在 PHP 中,如何保留 link 的第一部分,保留第二部分并动态更改其余部分
In PHP, how to preserve 1st part of a link, keep 2nd and dynamically change the rest
敌人的例子。让我们采用 4 个遵循以下目标模式的 URL(现有的或未来的):
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
所以。 如果 这些(现有的或未来的)link 被 单击,我们需要将它们更改为:
https://KeepThisPartTill-cde.com/a/b/cde/WhateverThisWillBe/ThisWillNot?SomethingHere
这意味着:
如果单击以 https://KeepThisPartTill-cde.com/a/b/cde/
开头的 link,保留此部分,添加部分 WhateverThisWillBe
(一直延伸到找到下一个 /
),添加 /ThisWillNot?
(注意 ?
)最后添加 SomethingHere
我应该使用 .htaccess
请记住,尽管它是关于 WordPress 网站的,但我可以计算出与 WordPress 相关的 SomethingHere
,但很难弄清楚如何在第二部分 [= 时保持第一部分完整无缺] 18=] 部分不同(在每个 link 中)并在其后添加 /ThisWillNot?
。
使用preg_replace:
preg_replace('~(https://KeepThisPartTill-cde.com/a/b/cde/).+?(/ThisWillNot)~i',
"WhateverThisWillBe?",
$string);
敌人的例子。让我们采用 4 个遵循以下目标模式的 URL(现有的或未来的):
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
所以。 如果 这些(现有的或未来的)link 被 单击,我们需要将它们更改为:
https://KeepThisPartTill-cde.com/a/b/cde/WhateverThisWillBe/ThisWillNot?SomethingHere
这意味着:
如果单击以 https://KeepThisPartTill-cde.com/a/b/cde/
开头的 link,保留此部分,添加部分 WhateverThisWillBe
(一直延伸到找到下一个 /
),添加 /ThisWillNot?
(注意 ?
)最后添加 SomethingHere
我应该使用 .htaccess
请记住,尽管它是关于 WordPress 网站的,但我可以计算出与 WordPress 相关的 SomethingHere
,但很难弄清楚如何在第二部分 [= 时保持第一部分完整无缺] 18=] 部分不同(在每个 link 中)并在其后添加 /ThisWillNot?
。
使用preg_replace:
preg_replace('~(https://KeepThisPartTill-cde.com/a/b/cde/).+?(/ThisWillNot)~i',
"WhateverThisWillBe?",
$string);