In PHP, how to preserve 1st part of a link, keep 2

2019-09-19 00:07发布

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条回答
老娘就宠你
2楼-- · 2019-09-19 00:39

Using preg_replace:

preg_replace('~(https://KeepThisPartTill-cde.com/a/b/cde/).+?(/ThisWillNot)~i',
             "$1WhateverThisWillBe$2?",
              $string);
查看更多
登录 后发表回答