删除特定字符的最后一个实例后的所有字符

How can I remove all the content in a string after the LAST occurance of a slash character / ?

For example, the string is:

http://localhost/new-123-rugby/competition.php?croncode=12345678

I want to remove all the content after the last / so that it just shows:

http://localhost/new-123-rugby/

But the content after the / could be of a variable length.

Please note, there could be any number of slashes in the URL. It needs to be able to remove content after the last slash. There could be more than shown in the example above.

NOTA:

The question was edited so that the original answer (below the edit), doesn't match the requirements from OP. It wasn't marked as an edit from OP.


EDIT:

Updated my answer so it now matches the requirements of OP:

(Now it works with as many slashes as you want)

Will also work using
http://localhost/new-123-rugby////////competition.php?croncode=12345678

<?php

    $url = "http://localhost/new-123-rugby/competition.php?croncode=12345678";
    echo dirname($url) . "/";

?>

Output:

http://localhost/new-123-rugby/

Original answer:

This should work for you:

<?php

    $string = "http://localhost/new-123-rugby/competition.php?croncode=12345678";
    echo $string = substr($string, 0, strpos(strrev($string), "/")-2);

?>

Output:

http://localhost/new-123-rugby/

Demo: http://ideone.com/0R9QUG

you can try this

$url = "http://localhost/new-123-rugby/competition.php?croncode=12345678";
preg_match("/[^\/]+$/", $url, $matches);
$newUrl = str_replace($matches[0],'',$url);
echo $newUrl;

Solution #1, using substr() + strrpos():

$string = 'http://localhost/new-123-rugby/competition.php?croncode=12345678';
$pos = strrpos($string, '/');
if ($pos !== FALSE) {
    echo(substr($string, 0, $pos + 1));
}

Function strrpos() finds the position of the last occurrence of / in the string, substr() extracts the required substring.

Drawback: if $string does not contain '/', strrpos() returns FALSE and substr() does not return what we want. Need to check the value returned by strrpos() first.

Solution #2, using explode() + implode():

$string = 'http://localhost/new-123-rugby/competition.php?croncode=12345678';
$array  = explode('/', $string);
if (count($array) > 1) {
    array_pop($array);               // ignore the returned value, we don't need it
    echo(implode('/', $array).'/');  // join the pieces back, add the last '/'
}

Alternatively, instead of array_pop($array) we can make the last component empty and there is no need to add an extra '/' at the end:

$string = 'http://localhost/new-123-rugby/competition.php?croncode=12345678';
$array  = explode('/', $string);
if (count($array) > 1) {
    $array[count($array) - 1] = '';  // empty the last component
    echo(implode('/', $array));  // join the pieces back
}

Drawback (for both versions): if $string does not contain '/', explode() produces an array containing a single value and the rest of the code produces either '/' (the first piece of code) or an empty string (the second). Need to check the number of items in the array produced by explode().

Solution #3, using preg_replace():

$string = 'http://localhost/new-123-rugby/competition.php?croncode=12345678';
echo(preg_replace('#/[^/]*$#', '/', $string));

Drawbacks: none. It works well when both when $string contains '/' and it does not contain '/' (it does not modify $string in this case).