2014-10-01 83 views
5

我有這條線。從後面找到第一個斜槓(文件路徑)

$line = '/opt/fings/interface/20140905111645811106.txt 0'; 

我用它來絆倒尾隨0/r/n

$pos = strpos($lines[$x], ' '); 
$file = '.'.substr($lines[$x], 0, $pos); 

所以我離開了這個/opt/fings/interface/20140905111645811106.txt

但我需要單獨的文件名。例如20140905111645811106.txt

我該從後面搶到字符串到第一次出現斜槓嗎?

+1

使用基本名()函數來獲取20140905111645811106.txt。例如echo basename($ line); – hizbul25 2014-10-01 08:09:21

+0

他們不叫反斜槓。 '/'是一個正斜槓。 – mario 2014-10-01 08:48:19

+0

@mario - 真 - 已更改 – morne 2014-10-01 08:56:08

回答

6

您可以使用basename()在這種情況下:

$line = '/opt/fings/interface/20140905111645811106.txt'; 
echo basename($line); // 20140905111645811106.txt 
+0

謝謝,這很方便。作品完美。 – morne 2014-10-01 08:08:10

+0

sure @mornenel im很高興這有幫助 – Ghost 2014-10-01 08:08:35

4

試試這個 -

$str = '/opt/fings/interface/20140905111645811106.txt'; 
$file = end(explode('/',$str)); 
echo $file; 

輸出將是 - 20140905111645811106.txt

+0

這工作100%一樣好,謝謝,但@Ghost第一次抱歉 – morne 2014-10-01 08:08:36

+0

多數民衆贊成在...你的歡迎:) – TBI 2014-10-01 08:09:48

3

三決定

echo pathinfo ('/opt/fings/interface/20140905111645811106.txt', PATHINFO_BASENAME ); 

pathinfo

2

您可以使用這一

$text = '/opt/fings/interface/20140905111645811106.txt' 
substr(strrchr($text, '/'), 1); 
2

的另一種方法,使用substrstrrpos

$filename = substr($line, strrpos($line, "/")+1); 
2

通過preg_match

$line = '/opt/fings/interface/20140905111645811106.txt'; 
preg_match('~[^/]+$~', $line, $match); 
echo $match[0]; // 20140905111645811106.txt 
相關問題