jay3ld Posted April 4, 2009 Share Posted April 4, 2009 Hello, I am creating a regular expression to match the output from the command "ls -go". I only really want the permissions and the file name. -rw-r--rw- 1 323 Nov 3 07:28 index.php I came up with this that works: ~([-d][-r][-w][-x][-r][-w][-x][-r][-w][-x])+\s+\d+\s+\d+\s+\d+\s+\d+\s+[A-Za-z]+\s+\d+\s+[\d:\d]+\s([A-Za-z0-9_\-]+.[A-Za-z]{3})+~mis I optimized it down to this: ~([-drwx]{10})+[\s\d]+[A-Za-z]+[\s\d:]+([A-Za-z0-9_\-.]+.[A-Za-z]{3})+~mis Any ideas on how i can optimize it any more than this? Did I miss something that my testing didn't see that could cause this to miss a true result? Quote Link to comment https://forums.phpfreaks.com/topic/152490-optimize-directory-listing-regex/ Share on other sites More sharing options...
nrg_alpha Posted April 4, 2009 Share Posted April 4, 2009 A quick and dirty way could include: $str = '-rw-r--rw- 1 323 Nov 3 07:28 index.php'; preg_match('#^([^ ]+).+?([^ ]+\..+)$#', $str, $match); echo $match[1] . ' ' . $match[2]; // outputs: -rw-r--rw- index.php But I suppose this largely depends on the nature of the string. Is the string in question by itself? Quote Link to comment https://forums.phpfreaks.com/topic/152490-optimize-directory-listing-regex/#findComment-800873 Share on other sites More sharing options...
nrg_alpha Posted April 4, 2009 Share Posted April 4, 2009 I suppose one could cover their whitespace bases by using \s instead of a literal space within those character classes.. #^([^\s]+).+?([^\s]+\..+)$# Quote Link to comment https://forums.phpfreaks.com/topic/152490-optimize-directory-listing-regex/#findComment-800903 Share on other sites More sharing options...
ghostdog74 Posted April 4, 2009 Share Posted April 4, 2009 why don't you use PHP readdir() and fileperms() ?? some things you just have to keep simple. Quote Link to comment https://forums.phpfreaks.com/topic/152490-optimize-directory-listing-regex/#findComment-801045 Share on other sites More sharing options...
Daniel0 Posted April 4, 2009 Share Posted April 4, 2009 You can use DirectoryIterator (or SplFileInfo for an individual file), which will give you an interface to all this. Not only will it be easier and faster, but it'll also be portable across other platforms. Quote Link to comment https://forums.phpfreaks.com/topic/152490-optimize-directory-listing-regex/#findComment-801049 Share on other sites More sharing options...
nrg_alpha Posted April 4, 2009 Share Posted April 4, 2009 Ah, nice to know about those functions. Learn something new every day! Quote Link to comment https://forums.phpfreaks.com/topic/152490-optimize-directory-listing-regex/#findComment-801256 Share on other sites More sharing options...
jay3ld Posted April 4, 2009 Author Share Posted April 4, 2009 nrg_alpha, No the string itself isn't in question. The output should always be similar to that. ghostdog74, The reason why I can't use those is that this information comes from a FTP connection (via a socket). I didn't see any information in FTP functions on getting the file permissions. Daniel0, Would those work with a remote socket connection to a FTP server? I have done a fsockopen to a FTP server, been able to authenticate, list files, change their permissions, etc. But I haven't found an easy way to just obtain the information I want (Their permissions) Quote Link to comment https://forums.phpfreaks.com/topic/152490-optimize-directory-listing-regex/#findComment-801424 Share on other sites More sharing options...
ghostdog74 Posted April 5, 2009 Share Posted April 5, 2009 ghostdog74, The reason why I can't use those is that this information comes from a FTP connection (via a socket). I didn't see any information in FTP functions on getting the file permissions. ok , so you are listing files from inside an FTP session? PHP has almost any kind of libraries you need to make life easy for you. check out the FTP module. you can use ftp_nlist() function to list files from remote server, then get the results and manipulate them as you wish. Quote Link to comment https://forums.phpfreaks.com/topic/152490-optimize-directory-listing-regex/#findComment-801481 Share on other sites More sharing options...
jay3ld Posted April 5, 2009 Author Share Posted April 5, 2009 ghostdog74, The reason why I can't use those is that this information comes from a FTP connection (via a socket). I didn't see any information in FTP functions on getting the file permissions. ok , so you are listing files from inside an FTP session? PHP has almost any kind of libraries you need to make life easy for you. check out the FTP module. you can use ftp_nlist() function to list files from remote server, then get the results and manipulate them as you wish. Correct, but it doesn't provide you with any other information that is useful such as permissions, ownership, modified time, etc. That is why I am doing it this way. This script isn't something I intend to release. It is being used on remote servers I have. The script has to be via FTP, as apache on that server doesn't run with enough permissions to the files/folders to to chmod. Quote Link to comment https://forums.phpfreaks.com/topic/152490-optimize-directory-listing-regex/#findComment-801703 Share on other sites More sharing options...
ghostdog74 Posted April 6, 2009 Share Posted April 6, 2009 Correct, but it doesn't provide you with any other information that is useful such as permissions, ownership, modified time, etc. That is why I am doing it this way. have you read the manual? see ftp_rawlist() function. Quote Link to comment https://forums.phpfreaks.com/topic/152490-optimize-directory-listing-regex/#findComment-802026 Share on other sites More sharing options...
ody3307 Posted April 13, 2009 Share Posted April 13, 2009 Assuming your test string is always in the format shown, below is probably the shortest regex. ([^\s]+) Permissions will be at array position 1 (the match is at position 0) and File name is at array position 7 Quote Link to comment https://forums.phpfreaks.com/topic/152490-optimize-directory-listing-regex/#findComment-808540 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.