Mombu the Php Forum sponsored links

Go Back   Mombu the Php Forum > Php > how to get a line of text as an array without white space?
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 25th March 22:33
lawrence krubner
External User
 
Posts: 1
Default how to get a line of text as an array without white space?


I'm suppose to track FTP uploads to the server. I've got a cron script,


lsof | grep vsftpd

This gives me a big file that has data like this in it:


vsftpd 12316 lawrence cwd DIR 9,2 4096
20938763 /data/ftp/lawrence_krubner

vsftpd 12316 lawrence rtd DIR 9,2 4096
20938763 /data/ftp/lawrence_krubner


Then I needed to this data broken up and stored in the database, each
field. I looped through this stuff, taking on line at a time, and each
line I would hit with explode, like this:

$arrayOfFields = explode(" ", $stringOfOneLine);


Unfortunetly, this gives me an array that has lots of blank rows because
every white space becomes a row in the array and, as you can see, there
is a lot of white space.

So I loop through the array and take out all the empty rows, but damn,
this is tedious and clumsy. Is there a better way?

I also struggle with the opposite problem:

Bee/878037001068/878037001068_01

The fields run together. Can anyone think of a way to separate them?
  Reply With Quote


  sponsored links


2 25th March 22:33
jerry stuckle
External User
 
Posts: 1
Default how to get a line of text as an array without white space?


Sounds like you're approaching things the wrong way. If you need to
track ftp uploads, you should be hooking into the ftpd daemon. You
won't be able to do it in PHP, though.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
  Reply With Quote
3 25th March 22:33
dale
External User
 
Posts: 1
Default how to get a line of text as an array without white space?


i'm going to stick to answering your question rather than telling you it
can't be done in php...like some other morons here.


not sure what you means by that one.

anyway, i couldn't tell if the output you pasted here is a single line entry
starting with 'vsftpd' and ending with 'lawrence_krubner' (case 'A') or, if
there were two related rows where a new row of data started after '4096' and
'lawrence_krubner' (case 'B'). there was a soft-return (a \n) after 4096
when i copied it into my test script...so, i assume the latter. if not, let
me know. either way, this should work...

=============

<?
$contents = "vsftpd 12316 lawrence rtd DIR 9,2
4096
20938763 /data/ftp/lawrence_krubner"; // case 'B'
$patterns = array(
'/\r?\n/' , // force unix \n
'/^/' , // beginning of line
'/$/' , // end of line
'/ +/' // spaces
);
$replacements = array(
"\"\n\"" , // unix nl w/ encapsulation
'"' , // start line with quote
'"' , // end line with quote
'", "' // encapsulate fields
);
$contents = preg_replace($patterns, $replacements, $contents);
?>

=============

this will turn your output file contents into a csv format. if case 'A', you
can use php's built-in cvs parsing functions to help you out or look at the
example below. if case 'B', you'll have to keep track of what row (even or
odd) you are processing to know what fields to expect from the row and what
positions the fields will be in.

i digress. at this point, you've gotten a format you can work with and
forced a unix \n to separate your rows. manually break it out into rows and
from there, into fields:

=============

<?
$contents = explode("\n", $contents); // file into array of rows
foreach ($contents as $row)
{
$row = substr($row, 1); // strip initial quote
$row = substr($row, 0, -1); // strip ending guote
$row = explode('", "', $row); // row into array of fields
foreach ($row as $index => $column) {
echo '<pre style="font:10px;">COLUMN ' . $index . ' = ' . $column .
'</pre>'; } }
?>

=============

if case 'A', this sample will output:

COLUMN 0 = vsftpd
COLUMN 1 = 12316
COLUMN 2 = lawrence
COLUMN 3 = rtd
COLUMN 4 = DIR
COLUMN 5 = 9,2
COLUMN 6 = 4096
COLUMN 7 = 20938763
COLUMN 8 = /data/ftp/lawrence_krubner

if case 'B', this sample will output:

COLUMN 0 = vsftpd
COLUMN 1 = 12316
COLUMN 2 = lawrence
COLUMN 3 = rtd
COLUMN 4 = DIR
COLUMN 5 = 9,2
COLUMN 6 = 4096
COLUMN 0 = 20938763
COLUMN 1 = /data/ftp/lawrence_krubner


anyway, like i said, i'm not making egotistical assumptions about what
you're trying to do. i'm merely answering the question you asked. i hope it
helps.

cheers
  Reply With Quote
4 25th March 22:33
michael fesser
External User
 
Posts: 1
Default how to get a line of text as an array without white space?


..oO(Lawrence Krubner)

Just curious: What does this tell you about FTP uploads? Additionally
lsof might output some more stuff than you actually want, so it might
make sense to pass some ******** command line options to it.

One option to remove empty array entries is array_filter(), but in this
case preg_split() would be better:

$arrayOfFields = preg_split('/ +/', $stringOfOneLine);

Where does this come from?

Micha
  Reply With Quote
5 25th March 22:33
c. (http://symcbean.blogspot.com/)
External User
 
Posts: 1
Default how to get a line of text as an array without white space?


What do you think you're measuring?

(because its not file uploads)

C.
  Reply With Quote
6 25th March 22:33
michael austin
External User
 
Posts: 1
Default how to get a line of text as an array without white space?


Exactly what are you measuring doing this? If this job runs every 5
minutes, what happens when an upload starts at minute 6 and completes at
minute 9?

lsof will only tell you the current open files, not files that may have
been recently uploaded.

And why not use the log files provided. See the following link for a
starting place...
http://mirror.centos.org/centos/4/docs/html/rhel-rg-en-4/s1-ftp-vsftpd-conf.html
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666