2013-04-21 47 views
0

如果我有以下网址:地带一切亚特第二斜线

http://www.mysite.com/games/topgame

如何使用PHP来删除后最后一个“/”一切吗?

所以我会留下:

http://www.mysite.com/games/

我已经试过这样:

$currentUrl = 'http://www.mysite.com/games/topgame';  
    $newstr = substr($currentUrl, 0, strpos($currentUrl, '/', strpos($currentUrl, '/')+4)); 
+0

Thanks..what如果我想之后的第二个 “/” 删除一切吗?所以它会让我与'http://www.mysite.com/'? – 2013-04-21 06:36:17

回答

0

试试这个代码...

$currentUrl = 'http://www.mysite.com/games/topgame';  
$newstr = substr($currentUrl, 0, strrpos($currentUrl, '/')); 

Codepad

+0

谢谢..如果我想删除第二个“/”后的所有内容,该怎么办?所以它会给我留下'mysite.com/'; ? – 2013-04-21 06:38:01

0
$newstr = substr($currentUrl, 0, strrchr($currentUrl, '/'); 
1

试试这个:

<?php 
print_r (dirname('http://www.mysite.com/games/topgame')); 
?> 
0

有多种功能,从路径挑出部分。

,如:

  • 基名() - 返回结尾的路径名组成部分
  • PATHINFO() - 返回文件路径
  • 真实路径)的信息( - 返回规范化的绝对​​路径名

但也许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