从给定的URL获取文件名部分

时间:2014-03-30 17:10:07

标签: php regex

我的网址如下:

http://abc.def.com/gh-tqa/name-of-the-file/ZW607CCF.html

现在我希望获得ZW607CCF的字符串,而不是.html

如何使用PHP实现这一目标?

3 个答案:

答案 0 :(得分:10)

这里不需要正则表达式。只需使用pathinfo()

$url = 'http://abc.def.com/gh-tqa/name-of-the-file/ZW607CCF.html';
$filename = pathinfo($url, PATHINFO_FILENAME);

答案 1 :(得分:0)

PHP中有很多方法,请尝试preg_replace(),例如:

$url = 'http://abc.def.com/gh-tqa/name-of-the-file/ZW607CCF.html';
$value = preg_replace("|.*/([^.]+)\.html|", "$1", $url);

答案 2 :(得分:0)

这是你的表现:

$temp = explode('.', $_SERVER['PHP_SELF']);

$string = $temp[0];

使用PHP 5.4+,你也可以这样做:

$string = explode('.', $_SERVER['PHP_SELF'])[0];
相关问题