Re: extracting data from text file
| From: | Richard Lynch | Date: | Mon, 01 Jul 2002 23:16:53 +0000 |
| Subject: | Re: extracting data from text file | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-104815@lists.php.net to get a copy of this message | ||
>Hello ,
>I have a text file that looks like this. each entry is on a different
>line I need to pull the data out of
>it and put each line of data into its own variable
>
>US
>State
>City
>Company Name
>Section 2
>www.domain.com
>admin@www.domain.com
>
>I have trend this but it does not work properly
>
> $fp = @fopen(file.txt,"r");
> $line = explode("\n",$fp);
> $valueC = "$line[0]";
> $valueST = "$line[1]";
> @fclose($fp);
$fp is a NUMBER.
The first file you open is Number 1, the second is number 2, and so on.
So you can't just go explode()ing $fp -- You've got to read some data.
$data = fread($fp, 10000); or
$data = fgets($fp, 100000000); or...
Lots of choices, actually, depending on how the file is layed out.
Now, some questions:
How "regimented" is the data? Is it *really* clean and always the same
number of lines?
If so, a simple:
$country = fgets($fp, 1000000);
$state = fgets($fp, 1000000);
.
.
.
inside of a while(!feof($fp)){ loop will work.
If there are sometimes some "missing" lines, or maybe sometimes two lines
where there should be one, it gets a bit more tricky...
You may need to look into http://php.net/strtok and do some
high-falutin'
artificial-intelligence analysis of the data coming it as you read it to
"guess" which line is what...
And definitely get rid of the @ symbol, or AT LEAST check the value of $fp
and do something intelligent if it's no good.
if (!$fp){
# send error message or whatver.
exit;
}
--
Like Music? http://l-i-e.com/artists.htm