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

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:


At 10:33 PM -0800 11/6/02, Salman wrote:

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:

drwxrwxrwx   1 owner    group               0 Nov  5 23:19 fantasy

to 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 fantasy

Notice 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

| Steve Edberg                                      [EMAIL PROTECTED] |
| University of California, Davis                          (530)754-9127 |
| Programming/Database/SysAdmin      |
| SETI@Home: 1001 Work units on 23 oct 2002                              |
| 3.152 years CPU time, 3.142 years SETI user... and STILL no aliens...  |

PHP General Mailing List (
To unsubscribe, visit:

Reply via email to