Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find first slash from the back ( file path )

Tags:

php

I have this line.

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

I used this to trip the trailing 0/r/n.

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

so I'm left with this /opt/fings/interface/20140905111645811106.txt

But I need the filename alone. e.g. 20140905111645811106.txt

Ho do I grab the string from the back up to the first occurrence of a slash?

like image 490
morne Avatar asked Oct 01 '14 08:10

morne


People also ask

Which way does slash go in file path?

For example, backslashes are used in non-relative path C:\Program Files (x86)\Microsoft Office\Office16. Yet, for a relative path, Windows adopts forward slashes. While in Mac, Linux, Android, Chrome, and Steam, all Unix-like operating systems, directories in file paths are separated by forward slashes.

Which path does not start with leading forward slash?

Because relative path names specify a path starting in the current directory, they do not begin with a slash (/). Relative path names are used to specify the name of a file in the current directory or the path name of a file or directory above or below the level of the current directory in the file system.

What is the difference between forward slash and backslash in file path?

If I recall correctly, backslash is used to denote something within the current computer or network such as C:\Windows or \\172.12. 1.34, while forward slashes are used to denote something that is outside or external to the current computer or network such as http://www.google.com/.

Does Windows use forward or backward slash?

Windows uses the backslash ( \ ) for the file system delimiter. For everything else the forward slash is used ( / ). The Uri type uses the forward slash because that is how a uniform resource identifier is defined.


1 Answers

You can use basename() in this case:

$line = '/opt/fings/interface/20140905111645811106.txt';
echo basename($line); // 20140905111645811106.txt
like image 149
Kevin Avatar answered Oct 02 '22 08:10

Kevin