If I have the following URL:
http://www.mysite.com/games/topgame
how do i use php to remove everything after the last "/"?
So I would be left with:
I have tried this:
$currentUrl = 'http://www.mysite.com/games/topgame';
$newstr = substr($currentUrl, 0, strpos($currentUrl, '/', strpos($currentUrl, '/')+4));
Try this code...
$currentUrl = 'http://www.mysite.com/games/topgame';
$newstr = substr($currentUrl, 0, strrpos($currentUrl, '/'));
See Codepad.
$newstr = substr($currentUrl, 0, strrchr($currentUrl, '/');
Try this:
<?php
print_r (dirname('http://www.mysite.com/games/topgame'));
?>
There are several functions to pick out parts from a path.
like:
but maybe pathinfo() is best suited for your needs:
<?php
$file = 'http://www.mysite.com/games/topgame/cool.php';
$path_parts = pathinfo($file);
echo $path_parts['dirname'] . "<br />";
echo $path_parts['basename'] . "<br />";
echo $path_parts['extension'] . "<br />";
echo $path_parts['filename'] . "<br />"; // since PHP 5.2.0
?>
Output:
http://www.mysite.com/games/topgame
cool.php
php
cool
Start searching here... http://se1.php.net/manual/en/function.pathinfo.php