PHP:仅更改 URL 字符串的一部分?

发布于 2024-10-07 17:09:19 字数 1008 浏览 10 评论 0原文

我正在开发一个小型 hoppy 项目,我想在其中替换 URL 上的特定页面。让我解释一下:

我有网址

http://www.example.com/article/paragraph/low/

我想要保留 URL,但将最后一段 /low/ 替换为 /high/,因此新 URL 为:

http://www.example.com/article/paragraph/high/

我我尝试过不同的爆炸、分裂和拼接,但我似乎无法理解它并使其发挥作用。我可以更改整个 URL,而不仅仅是最后一段,并将其保存在新变量中。
我非常有信心这是一个非常简单的情况,但我从来没有在 PHP 中进行过那么多的数组/字符串操作,所以我很迷失。

我想我必须首先将 URL 分段,使用“\”将其分隔(我尝试过,但使用 explode("\", $string)) 时遇到问题,然后将最后一个 \low\ 替换为 \high\

希望有人可以帮助或为我指出正确的方向,以使用什么方法来执行此操作。

真诚
梅斯蒂卡

I’m working on a small hoppy project where I want to replace a specific page on a URL. Let me explain:

I’ve got the URL

http://www.example.com/article/paragraph/low/

I want to keep the URL but replace the last segment /low/ with /high/ so the new URL is:

http://www.example.com/article/paragraph/high/

I’ve tried different explode, split and splice but I just can’t seem to wrap my head around it and make it work. I can change the entire URL but not just the last segment and save it in a new variable.
I’m pretty confidence that it is a pretty straight forward case but I’ve never worked that much with arrays / string-manipulation in PHP so I’m pretty lost.

I guess that I have to first split the URL up in segments, using the "\" to separate it (I tried that but have problems by using explode("\", $string)) and then replace the last \low\ with \high\

Hope someone could help or point me in the right direction to what methods to use for doing this.

Sincere
Mestika

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

多孤肩上扛 2024-10-14 17:09:19

str_replace 怎么样?

<?php
$newurl = str_replace('low', 'high', $oldurl);
?>

文档;
http://php.net/manual/en/function.str-replace.php编辑


里克是对的;如果您的域名(或 URL 的任何其他部分)包含字符串“low”,这会弄乱您的链接。
因此:如果您的网址可能包含多个“low”,则必须在脚本中添加额外的指示符。一个例子是在 str_replace 中包含 /。

how about str_replace?

<?php
$newurl = str_replace('low', 'high', $oldurl);
?>

documentation;
http://php.net/manual/en/function.str-replace.php

edit;
Rik is right; if your domain (or any other part of the url for that matter) includes the string "low", this will mess up your link.
So: if your url may contain multiple 'low' 's, you will have to add an extra indicator in the script. An example of that would be including the /'s in your str_replace.

少女的英雄梦 2024-10-14 17:09:19

你把 \ 当作 / 了。

$url = explode('/', rtrim($url, '/'));
if (end($url) == 'low') {
    $url[count($url)-1] = 'high';
}
$url = implode('/', $url) .'/';

You took \ for /.

$url = explode('/', rtrim($url, '/'));
if (end($url) == 'low') {
    $url[count($url)-1] = 'high';
}
$url = implode('/', $url) .'/';
神回复 2024-10-14 17:09:19

使用 parse_url 将 URL 拆分为各个组件,根据需要进行修改(这里可以使用 explode 将路径分割成多个段),然后使用 < a href="http://php.net/http_build_url" rel="nofollow">http_build_url

Use parse_url to split the URL into its components, modify them as required (here you can use explode to split the path into its segments), and then rebuild the URL with http_build_url.

水晶透心 2024-10-14 17:09:19
<?php

class TestURL extends PHPUnit_Framework_TestCase {
    public function testURL() {
        $URL =  'http://www.mydomain.com/article/paragraph/low/';
        $explode = explode('/', $URL);
        $explode[5] = 'high';
        $expected = 'http://www.mydomain.com/article/paragraph/high/';
        $actual = implode('/', $explode);
        $this->assertEquals($expected, $actual);
    }
}

--

phpunit simple-test.php 
PHPUnit 3.4.13 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 4.75Mb

OK (1 test, 1 assertion)
<?php

class TestURL extends PHPUnit_Framework_TestCase {
    public function testURL() {
        $URL =  'http://www.mydomain.com/article/paragraph/low/';
        $explode = explode('/', $URL);
        $explode[5] = 'high';
        $expected = 'http://www.mydomain.com/article/paragraph/high/';
        $actual = implode('/', $explode);
        $this->assertEquals($expected, $actual);
    }
}

--

phpunit simple-test.php 
PHPUnit 3.4.13 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 4.75Mb

OK (1 test, 1 assertion)
楠木可依 2024-10-14 17:09:19

这可能就足够了:

$url = "http://www.mydomain.com/article/paragraph/low/";
$newUrl = str_replace('/low/', '/high/', $url);

或者使用正则表达式(它允许更大的灵活性)

$url = "http://www.mydomain.com/article/paragraph/low/";
$newUrl = preg_replace('/low(\/?)$/', 'high$1', $url);

请注意,字符串方法将替换任何 low 段,并且仅当它后面跟着 / 时。仅当 low 是最后一段并且后面不能跟 / 时,正则表达式方法才会替换它。

This will probably be enough:

$url = "http://www.mydomain.com/article/paragraph/low/";
$newUrl = str_replace('/low/', '/high/', $url);

or with regular expressions (it allows more flexibility)

$url = "http://www.mydomain.com/article/paragraph/low/";
$newUrl = preg_replace('/low(\/?)$/', 'high$1', $url);

Note that the string approach will replace any low segment and only if it's followed by a /. The regex approach will replace low only if it's the last segment and it may not be followed by a /.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文