34

So the test case string may be:

http://example.com/?u=ben

Or

http://example.com

I'm trying to remove everything after the last occurrence of a '/' but only if it's not part of the 'http://'. Is this possible!?

I have this so far:

$url = substr($url, 0, strpos( $url, '/'));

But does not work, strips off everything after first '/'.

Jeff Hines
  • 3,511
  • 1
  • 19
  • 21
benhowdle89
  • 36,900
  • 69
  • 202
  • 331
  • I don't know what you're using this for but if you're attempting to strip campaign tracking codes, the resulting URLs will usually work but not always ;-) For example, I think YouTube needs the "?" – PJ Brunet Oct 03 '13 at 20:31

4 Answers4

120

You must use strrpos function not strpos ;-)

substr($url, 0, strrpos( $url, '/'));
user2422121
  • 1,209
  • 2
  • 8
  • 3
  • 7
    I think this is the best answer to the title of the question! I would edit the title to something like "...character in a URL" if it were not for this answer. – PJ Brunet Oct 03 '13 at 20:25
  • 1
    if no occurrence found it removes all, any modificaton for this – mokNathal Apr 02 '16 at 09:54
  • @mokNathal it's ugly but... `substr($url,0,strrpos($url,'/') !== false ? strrpos($url,'/') : strlen($url));` – But those new buttons though.. Jul 07 '16 at 21:06
  • slightly less ugly but fails when the needle is in the first position: `substr($url,0,strrpos($url,'/') ?: strlen($url));` – But those new buttons though.. Jul 07 '16 at 21:09
  • It should be noted that strrpos returns false if no occurrence of your needle is found. If you think that the needle may no be present, check that it exists before executing this instruction. – Mosset Jérémie Mar 20 '19 at 14:55
  • This is *technically* incorrect, as the OP was to remove chars *after* the last slash (meaning, the slash itself should not be removed). So `substr($url, 0, strrpos( $url, '/')+1);` would be correct. – Skeets Jan 14 '22 at 05:42
16

You should use the tool that is designed for this type of job, parse_url

url.php

<?php

$urls = array('http://example.com/foo?u=ben',
                'http://example.com/foo/bar/?u=ben',
                'http://example.com/foo/bar/baz?u=ben',
                'https://foo.example.com/foo/bar/baz?u=ben',
            );


function clean_url($url) {
    $parts = parse_url($url);
    return $parts['scheme'] . '://' . $parts['host'] . $parts['path'];
}

foreach ($urls as $url) {
    echo clean_url($url) . "\n";
}

Example:

·> php url.php                                                                                                 
http://example.com/foo
http://example.com/foo/bar/
http://example.com/foo/bar/baz
https://foo.example.com/foo/bar/baz
sberry
  • 128,281
  • 18
  • 138
  • 165
3

Actually, a simpler solution to what you want to achieve is to play with a few string manipulation functions of PHP.

First of all, you need to find the position of the last occurrence of the '/'. You can do this by using the strrpos() function (be careful, it is with 2 r);

Then, if you provide this position as a negative value, as a second parameter to the substr() function, it will start the search of the substring from the end.

The second problem is that you want as result the part of the strings that is at the LEFT of the last '/'. To achieve this, you will have to provide substr() with a negative value to the third parameter which will indicate how many characters you want to remove.

To be sure of how many parameters you need to remove, you will have to extract the string part at the RIGHT of the '/' first, and then count its length.

//so given this url:
$current_url = 'http://example.com/firstslug/84'

//count how long is the part to be removed
$slug_tbr = substr($current_url, strrpos($current_url, '/')); // '/84'

$slug_length = strlen(slug_tbr); // (3)

/*get the final result by giving a negative value 
to both second and third parameters of substr() */
$back_url = substr($current_url, -strrpos($current_url, '/'), -$slug_length);

//result will be http://example.com/firstslug/
omkaartg
  • 2,676
  • 1
  • 10
  • 21
Lanci
  • 530
  • 5
  • 8
0
$cutoff = explode("char", $string); 
echo $cutoff[0]; // 2 for what you want and 3 for the index

also

echo str_replace("http://", "", $str);

Seb
  • 100
  • 6