Hi,
I've had a problem I've been trying to solve for the past couple days but I haven't gotten anywhere so hopefully someone here can help me out.
Basically, I'm trying to read in a .txt file so that I can parse it. I have been able to read it, but the problem is that I am losing lots of whitespace. spaces over 1 space are reduced to 1 space. So their could be 10 spaces between 2 pieces of data but they are always reduced to 1 space. This file is organized into strict columns so the easiest way to parse it is with substr (if whitespace is preserved) because data1 is always substr($x, 0, 20), data2 is always substr($x,21,25), etc...
First, I was trying to read it from a URL...but thought maybe PHP was trying to read it as html so I copied it to my server through PHP copy. I downloaded the file locally and it still had the whitespace so copy() in PHP didn't lose the whitespace. Then I tried again to read in the file but it was the same thing. In addition to file_get_contents I've also tried fread().
Here's the latest code I was using:
ini_set('allow_url_fopen','1');
$fp = fopen("weeklystats/stats.txt", 'r');
$contents = file_get_contents("stats.txt");
$array=explode("\n", $contents);
foreach($array as $val)
{
echo "$val";
}
echo "$contents";
@fclose($fp);
When I view source on the page it looks like the whitespace has been preserved, but when I try to parse it with substr() the columns are way off. Of course I know that my browser will chop consecutive spaces down to a single space, but I would think I could read the file in with the spaces preserved.
Here's the file I'm reading in if anyone wants to try it themselves.
Thanks!
jason
I've had a problem I've been trying to solve for the past couple days but I haven't gotten anywhere so hopefully someone here can help me out.
Basically, I'm trying to read in a .txt file so that I can parse it. I have been able to read it, but the problem is that I am losing lots of whitespace. spaces over 1 space are reduced to 1 space. So their could be 10 spaces between 2 pieces of data but they are always reduced to 1 space. This file is organized into strict columns so the easiest way to parse it is with substr (if whitespace is preserved) because data1 is always substr($x, 0, 20), data2 is always substr($x,21,25), etc...
First, I was trying to read it from a URL...but thought maybe PHP was trying to read it as html so I copied it to my server through PHP copy. I downloaded the file locally and it still had the whitespace so copy() in PHP didn't lose the whitespace. Then I tried again to read in the file but it was the same thing. In addition to file_get_contents I've also tried fread().
Here's the latest code I was using:
ini_set('allow_url_fopen','1');
$fp = fopen("weeklystats/stats.txt", 'r');
$contents = file_get_contents("stats.txt");
$array=explode("\n", $contents);
foreach($array as $val)
{
echo "$val";
}
echo "$contents";
@fclose($fp);
When I view source on the page it looks like the whitespace has been preserved, but when I try to parse it with substr() the columns are way off. Of course I know that my browser will chop consecutive spaces down to a single space, but I would think I could read the file in with the spaces preserved.
Here's the file I'm reading in if anyone wants to try it themselves.
Thanks!
jason