Re: Regular Expression
| From: | Steve Edberg | Date: | Thu, 07 Nov 2002 14:53:40 +0000 |
| Subject: | Re: Regular Expression | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-123162@lists.php.net to get a copy of this message | ||
As someone else mentioned, filenames with embedded spaces can be confusing and should be avoided where possible (in the future, I'd replace the space with an underscore _). However, the following will do what you want (untested):
$regs = preg_split('/\w+/', $dirline, 9);
$regs[8] will contain the filename, even if it has spaces. Explanation: the regex splits the line on strings of whitespace (spaces, tabs, etc.), for a maximum of 9 elements. Since $regs[8], containing the filename, is the ninth element, the rest of the line is dumped into that element without further regex processing.
Since the spacing pattern remains the same from a unix ls -l command no matter what the file size or date is, you should be ok. See
http://php.he.net/manual/en/function.preg-split.php
for more info.
Also, if you are doing something like
$output =
ls -l;
and then parsing the output you may want to investigate PHP's file/directory functions instead:
http://php.he.net/manual/en/ref.dir.php
http://php.he.net/manual/en/ref.filesystem.php
-steve
At 10:33 PM -0800 11/6/02, Salman wrote:
Hi, I have this ereg call ereg("([-d])[rwxst-]{9}.* [0-9]* [a-zA-Z]+ [0-9: ]* (.+)",$dirline,$regs); This regular expressions parses the following line:-- +------------------------------------------------------------------------+ | Steve Edberg sbedberg@ucdavis.edu | | University of California, Davis (530)754-9127 | | Programming/Database/SysAdmin http://pgfsun.ucdavis.edu/ | +------------------------------------------------------------------------+ | SETI@Home: 1001 Work units on 23 oct 2002 | | 3.152 years CPU time, 3.142 years SETI user... and STILL no aliens... | +------------------------------------------------------------------------+drwxrwxrwx 1 owner group 0 Nov 5 23:19 fantasyto return: fantasy (or in general any filename/directory name) The above regular expression works fine in every except when the filename is something like this:drwxrwxrwx 1 owner group 0 Nov 5 23:19 6 fantasyNotice in this case the filename is "6 fantasy" however $regs[1] returns only: "fantasy" Can anyone help me to fix this problem. Thanks a bunch