问题描述
敌人的例子.让我们采用 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
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
所以.如果这些(现有的或未来的)链接中的任何一个被点击,我们需要将它们更改为:https://KeepThisPartTill-cde.com/a/b/cde/WhateverThisWillBe/ThisWillNot?SomethingHere
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
这意味着:
如果单击以 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
我应该使用 .htaccess
请记住,虽然它是关于 WordPress 网站的,但我可以计算出与 WordPress 相关的 SomethingHere
,但很难弄清楚如何在第二个 SomethingHere
时保持第一部分的完整性code>WhateverThisWillBe 部分是不同的(在每个链接中)并在其后添加 /ThisWillNot?
.
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.
推荐答案
使用 preg_replace:
Using preg_replace:
preg_replace('~(https://KeepThisPartTill-cde.com/a/b/cde/).+?(/ThisWillNot)~i',
"$1WhateverThisWillBe$2?",
$string);
这篇关于在 PHP 中,如何保留链接的第一部分,保留第二部分并动态更改其余部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!