3

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

Emtiaz Zahid
  • 2,645
  • 2
  • 19
  • 35
Emil Devantie Brockdorff
  • 4,724
  • 12
  • 59
  • 76

5 Answers5

4

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.

DamnYankee
  • 86
  • 1
  • 5
3

You took \ for /.

$url = explode('/', rtrim($url, '/'));
if (end($url) == 'low') {
    $url[count($url)-1] = 'high';
}
$url = implode('/', $url) .'/';
rik
  • 8,592
  • 1
  • 26
  • 21
1
<?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)
Alfred
  • 60,935
  • 33
  • 147
  • 186
  • 1
    @using `split` "This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged." – Alin Purcaru Dec 14 '10 at 16:00
  • Thanks Alin I did not know that + phpunit did not show me the warning. Thanks for the info. I updated the snippet. – Alfred Dec 14 '10 at 16:31
1

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.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • I don't think using `parse_url` simplifies his problem. It's a very simple problem already, but he just doesn't know how to approach it. – Alin Purcaru Dec 14 '10 at 15:58
  • 2
    @Alin Purcaru: It’s not about simplification but about correctness. – Gumbo Dec 14 '10 at 15:59
  • I guess it would make a bit of sense to use `parse_url` if he wants to use an `explode` approach. I had in mind a replace solution and for that it would be useless to change the url in any way. – Alin Purcaru Dec 14 '10 at 16:03
0

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 /.

Alin Purcaru
  • 43,655
  • 12
  • 77
  • 90