如果我有以下网址:
http://www.mysite.com/games/topgame
如何使用php删除最后一个“/”之后的所有内容?
所以我会留下:
我试过这个:
$currentUrl = 'http://www.mysite.com/games/topgame';
$newstr = substr($currentUrl, 0, strpos($currentUrl, '/', strpos($currentUrl, '/')+4));
答案 0 :(得分:1)
试试这个:
<?php
print_r (dirname('http://www.mysite.com/games/topgame'));
?>
答案 1 :(得分:0)
试试这段代码......
$currentUrl = 'http://www.mysite.com/games/topgame';
$newstr = substr($currentUrl, 0, strrpos($currentUrl, '/'));
请参阅 Codepad 。
答案 2 :(得分:0)
$newstr = substr($currentUrl, 0, strrchr($currentUrl, '/');
答案 3 :(得分:0)
从路径中挑选部件有几个功能。
像:
但 pathinfo() 最适合您的需求:
<?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
?>
输出:
http://www.mysite.com/games/topgame
cool.php
php
cool
开始在此搜索... http://se1.php.net/manual/en/function.pathinfo.php