Extracting Specific Piece of Words from a Line

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
devarishi
Forum Contributor
Posts: 101
Joined: Fri Feb 05, 2010 7:15 pm

Extracting Specific Piece of Words from a Line

Post by devarishi »

Hi,


First, consider this PHP Script:

Code: Select all

-bash-2.05b# cat test.php
<?php

        $line1 = '-rwxr-xr-x  1 root root 125 Sep  2 16:40 viewTmp.sh';
        $line2 = '-rwxr-xr-x  1 root root 125 Sep  2 16:40 August 2010';

        $words1 = explode(" ",$line1);
        echo "\$line1 = " ; var_dump($words1);

        $words2 = explode(" ",$line2);
        echo "\n\$line2 = " ; var_dump($words2);

        $fileName1 = $words1[count($words1) - 1];
        echo "\nFile Name-1: " . $fileName1;

        $fileName2 = $words2[count($words2) - 1];
        echo "\nFile Name-2: " . $fileName2;

        echo "\n";
?>
-bash-2.05b#

Please mind the first two statements which contains file names in them at the end. The first file name is "viewTmp.sh" whereas the second file name consists of two separate words "August 2010":

Code: Select all

$line1 = '-rwxr-xr-x  1 root root 125 Sep  2 16:40 viewTmp.sh';
$line2 = '-rwxr-xr-x  1 root root 125 Sep  2 16:40 August 2010';

I want to extract only file names from those lines. Of course, they are going to come from the output of this UNIX statement:

Code: Select all

ls -l
But in the example, I have simply mentioned them directly.

Okay, the problem is that I am able to extract the last word but what if there are two or more words separated by white spaces that form a file / directory name?


Here's the output of the above PHP Script:

Code: Select all

-bash-2.05b# php test.php
Content-type: text/html
X-Powered-By: PHP/4.3.10

$line1 = array(11) {
  [0]=>
  string(10) "-rwxr-xr-x"
  [1]=>
  string(0) ""
  [2]=>
  string(1) "1"
  [3]=>
  string(4) "root"
  [4]=>
  string(4) "root"
  [5]=>
  string(3) "125"
  [6]=>
  string(3) "Sep"
  [7]=>
  string(0) ""
  [8]=>
  string(1) "2"
  [9]=>
  string(5) "16:40"
  [10]=>
  string(10) "viewTmp.sh"
}

$line2 = array(12) {
  [0]=>
  string(10) "-rwxr-xr-x"
  [1]=>
  string(0) ""
  [2]=>
  string(1) "1"
  [3]=>
  string(4) "root"
  [4]=>
  string(4) "root"
  [5]=>
  string(3) "125"
  [6]=>
  string(3) "Sep"
  [7]=>
  string(0) ""
  [8]=>
  string(1) "2"
  [9]=>
  string(5) "16:40"
  [10]=>
  string(6) "August"
  [11]=>
  string(4) "2010"
}

File Name-1: viewTmp.sh
File Name-2: 2010
-bash-2.05b#

As it can be seen above that the second file name is being taken as 2010 instead of August 2010:

Code: Select all

File Name-2: 2010
The file name August 2010 is being split (exploded) into two separate words. How to resolve this problem?
TipPro
Forum Commoner
Posts: 35
Joined: Wed Mar 15, 2006 6:39 pm

Re: Extracting Specific Piece of Words from a Line

Post by TipPro »

Not sure what you are doing, but if the input line is always in the same pattern you can just collect everything 3 chars after the ':'

Code: Select all

$fileName = substr($line, strpos($line, ':')+4);
devarishi
Forum Contributor
Posts: 101
Joined: Fri Feb 05, 2010 7:15 pm

Re: Extracting Specific Piece of Words from a Line

Post by devarishi »

TipPro wrote:Not sure what you are doing, but if the input line is always in the same pattern you can just collect everything 3 chars after the ':'

Code: Select all

$fileName = substr($line, strpos($line, ':')+4);

I guessed I would need to use substr() but didn't think of strpos() that along with substr() it would do the work.

Thank you so much!

I guess, you didn't need to understand what I was trying to do. But you got hit the point without failing!

But I tell you in brief: Output of "ls -l" from a Shell Script program is passed to a PHP Script. It displays the output it receives and also creates a link to file names so that when we click those file names, we can further take an action on them. For example, displaying their contents. I have done it using "ls -1" temporarily because it displays only file names on separate lines.

So, now you got my purpose? :drunk:

Thanks again!
devarishi
Forum Contributor
Posts: 101
Joined: Fri Feb 05, 2010 7:15 pm

Re: Extracting Specific Piece of Words from a Line

Post by devarishi »

TipPro wrote:Not sure what you are doing, but if the input line is always in the same pattern you can just collect everything 3 chars after the ':'

Code: Select all

$fileName = substr($line, strpos($line, ':')+4);

Hi,


There is a problem. I just found out that the colon ":" is missing in HH:MM field for some directories:

Code: Select all

-bash-2.05b# ls -l /
total 192
drwxr-xr-x   2 root root   4096 Feb 17  2005 bin
drwxr-xr-x   4 root root   4096 Mar 29  2005 boot
drwxr-xr-x  24 root root 118784 Sep  5 16:09 dev
drwxr-xr-x  69 root root   8192 Sep  5 16:09 etc
drwxr-xr-x   4 root root   4096 Sep  3 12:57 home
drwxr-xr-x   9 root root   4096 Feb 17  2005 lib
drwx------   2 root root  16384 Jul 27  2004 lost+found
drwxr-xr-x   2 root root   4096 Apr 14  2004 misc
drwxr-xr-x   2 root root   4096 Jul 27  2004 mnt
dr-xr-xr-x  47 root root      0 Sep  5 16:09 proc
drwxr-xr-x   7 root root   4096 Sep  3 13:22 root
drwxr-xr-x   2 root root   8192 Feb 17  2005 sbin
drwxr-xr-x   4 root root      0 Sep  5 16:09 selinux
drwxr-xr-x  10 root root      0 Sep  5 16:09 sys
drwxrwxrwt   3 root root   4096 Sep  5 16:09 tmp
drwxr-xr-x  14 root root   4096 Jul 22  2004 usr
drwxr-xr-x  21 root root   4096 Jul 22  2004 var
These directories, for example, have time-stamps that don't include a colon ":" in the "HH:MM" field just before their names.

Code: Select all

drwxr-xr-x   9 root root   4096 Feb 17  2005 lib
drwx------   2 root root  16384 Jul 27  2004 lost+found
drwxr-xr-x   2 root root   4096 Apr 14  2004 misc
drwxr-xr-x   2 root root   4096 Jul 27  2004 mnt

So, this solution doesn't work in the above exceptional cases:

Code: Select all

$fileName = substr($line, strpos($line, ':')+4);
We need to figure out some other patter match.
devarishi
Forum Contributor
Posts: 101
Joined: Fri Feb 05, 2010 7:15 pm

Re: Extracting Specific Piece of Words from a Line

Post by devarishi »

Okay, I found out the solution:

-bash-2.05b# ls -l --full-time --time-style=long-iso /
total 192
drwxr-xr-x 2 root root 4096 2010-09-05 18:59 bin
drwxr-xr-x 4 root root 4096 2010-09-05 16:20 boot
drwxr-xr-x 24 root root 118784 2010-09-05 21:21 dev
drwxr-xr-x 69 root root 8192 2010-09-05 21:20 etc
drwxr-xr-x 4 root root 4096 2010-09-03 12:57 home
drwxr-xr-x 9 root root 4096 2005-02-17 23:32 lib
drwx------ 2 root root 16384 2004-07-27 12:59 lost+found
drwxr-xr-x 2 root root 4096 2004-04-14 18:39 misc
drwxr-xr-x 2 root root 4096 2004-07-27 13:38 mnt
dr-xr-xr-x 46 root root 0 2010-09-05 21:20 proc
drwxr-xr-x 7 root root 4096 2010-09-03 13:22 root
drwxr-xr-x 2 root root 8192 2005-02-17 23:33 sbin
drwxr-xr-x 4 root root 0 2010-09-05 21:20 selinux
drwxr-xr-x 10 root root 0 2010-09-05 21:20 sys
drwxrwxrwt 3 root root 4096 2010-09-05 21:21 tmp
drwxr-xr-x 14 root root 4096 2004-07-22 12:46 usr
drwxr-xr-x 21 root root 4096 2004-07-22 13:00 var
-bash-2.05b#
Post Reply