Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

How to seperate data from line in txt file

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

How to seperate data from line in txt file

Postby phantom_turtle » Tue Jan 07, 2003 6:55 pm

I want to know if it is possible to seperate data from a line in a text file. For example:

Text file contains the folowing lines:

somedata somedata somedata somenumber
somedata somedata somedata somenumber
somedata somedata somedata somenumber
somedata somedata somedata somenumber

Like a log.

What I want to be able to do is read a line from the file and just get the "somenumber" bit on the end and assign it to a variable to use in the php script.

Code: Select all
$variable = "somenumber"


For instance so that you can add up all the "somenumbers" at the end of the lines and have a total of all the some numbers for the whole text file.

Any suggestions or code would be greatly apreciated.


phantom_turtle
phantom_turtle
New php-forum User
New php-forum User
 
Posts: 2
Joined: Thu Nov 07, 2002 9:10 pm
Location: australia

Postby Xer0 » Wed Jan 08, 2003 2:14 am

Depending on your data, you could use sscanf() if you know the format (eg "%s %s %s %i") or possibly split() to put the data into an array, and just use the last value of that array.

Hope this helps

- Xer0
Xer0
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Jan 06, 2003 9:27 pm

Postby meannate » Thu Jan 16, 2003 2:50 pm

I would imagine the data in your datafile is separated by a colon, coma, or maybe a space... you say your interested only in the last item.

Code: Select all
$datafilename = "datafile.log";
$datafile = fopen($datafilename, "r") or die("Couldn't Open");
$data = fread($datafile, filesize($datafilename) or die ("Couldn't Read");
fclose($datafile);

$data = split("\r", $data);

foreach ($data as $dataline) {
  //STRRCHR SEARCHES FOR LAST OCCURANCE OF ':' AND RETURNS THE REST OF THE STRING
  $THIS = strrchr($dataline, ":");

  echo $THIS;
}


-Nathan
meannate
New php-forum User
New php-forum User
 
Posts: 8
Joined: Mon Nov 04, 2002 12:06 pm
Location: West Los Angeles


Return to PHP coding => General

Who is online

Users browsing this forum: Baidu [Spider] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron