2009-11-04 134 views
0

我试过功能:strstr,但它有一个问题。假设,网址是这样的:删除URL部分和文件扩展名与strstr

http://www.example.com/index.php 

用的strstr,我能够删除之前“/”什么,但我只想:

指数

即文件的实际名称没有扩展。

回答

3

如果它总是以.php结束,你可以这样做:

basename('http://www.example.com/index.php', '.php') 

如果它能够与其他扩展名结尾,你可以这样做:

if (preg_match('#([^/]+)\.\w+$#', 'http://www.example.com/index.php', $matches)) 
    $basename = $matches[1]; 
+0

@reko_t:它有时可能以.html结尾。 – RKh 2009-11-04 10:04:26

+0

编辑我的答案考虑到这一点。 – 2009-11-04 11:48:54

6

我会强烈建议使用PHP parse_url()功能:

$address = 'http://www.example.com/index.php'; 
$url = parse_url($address); 
echo $url['host']; 

没有点重新发明轮子。

4

如果文件类型可以改变,你是确定没有其他。在文件名中例如index.2.php那么你可以使用

$filename = basename('http://www.example.com/index.php'); 
$filename = substr($filename, 0, strpos($filename, '.')); 
3

+1克莱图斯为合适的工作的正确工具,正确的URL解析器。正则表达式黑客将失败的各种查询字符串的东西。

然而,它是这里寻找的不是主人的最后路径部分。所以:

$url = parse_url($address); 
$filename= array_pop(explode('/', $url['path'])); 
$filestem= explode('.', $filename)[0]; 
0

它很简单,不需要太多代码。

只是做手工,它会自动readed

只写你的网址一样,

 http://www.example.com/index 

,它会显示你的文件,因为名为.php到底。