如果我有以下网址:地带一切亚特第二斜线
http://www.mysite.com/games/topgame
如何使用PHP来删除后最后一个“/”一切吗?
所以我会留下:
我已经试过这样:
$currentUrl = 'http://www.mysite.com/games/topgame';
$newstr = substr($currentUrl, 0, strpos($currentUrl, '/', strpos($currentUrl, '/')+4));
如果我有以下网址:地带一切亚特第二斜线
http://www.mysite.com/games/topgame
如何使用PHP来删除后最后一个“/”一切吗?
所以我会留下:
我已经试过这样:
$currentUrl = 'http://www.mysite.com/games/topgame';
$newstr = substr($currentUrl, 0, strpos($currentUrl, '/', strpos($currentUrl, '/')+4));
试试这个代码...
$currentUrl = 'http://www.mysite.com/games/topgame';
$newstr = substr($currentUrl, 0, strrpos($currentUrl, '/'));
见Codepad。
谢谢..如果我想删除第二个“/”后的所有内容,该怎么办?所以它会给我留下'mysite.com/'; ? – 2013-04-21 06:38:01
$newstr = substr($currentUrl, 0, strrchr($currentUrl, '/');
试试这个:
<?php
print_r (dirname('http://www.mysite.com/games/topgame'));
?>
有多种功能,从路径挑出部分。
,如:
但也许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
Thanks..what如果我想之后的第二个 “/” 删除一切吗?所以它会让我与'http://www.mysite.com/'? – 2013-04-21 06:36:17