Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Losing consecutive spaces on .txt Read File 2

Status
Not open for further replies.

jay123454

Programmer
Jun 15, 2001
46
US
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
 
<?PHP
$fp = fopen("weeklystats/stats.txt", 'r');
$contents = file_get_contents("weeklystats/stats.txt");
$array=explode("\n", $contents);
foreach($array as $val)
{
//echo "$val";
}
//echo "$contents";
echo "<pre>";
print_r($array);
@fclose($fp);
?>

Please try this one!

You will see what the array holds!

also I use <pre> in order to display the whitespaces also!
with <pre> you browser will display all spaces!

[5] => Abdul-Jabbar MIA 1289 RB 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[6] => Abdullah, R TB 1584 RB 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0


the browser give me something like that!

So you have the colums just like you want them right?

But I can not understand what are you trying to do.

You just create an array with each line of the text!
Is that it?

Hope it helps you :)
 
or:
echo str_replace(" ","&nbsp;",$val);

Known is handfull, Unknown is worldfull
 
Thanks guys,

yeah, fire...i'm just creating an array with each line of text. Each line in the array will be a player's stats. Then I plan looping through the array using substr() to get the stats for each player. So as long as all the columns line up so for example, passing yards are always substr($line, 20, 23).
I didn't make the format..don't know why they don't do csv or tab delimited, it's not the best but it will work for now. There are that many places out there to get stats for weekly NFL games and this place was the best all around for price and quality/quantity of stats.

Just working on a program to save me some time from entering the stats by hand for our fantasy football league.


I'll give those a try and see if it works, but if the spaces are preserved that's all I need!


Will let you know if that helps. Thanks for the help!
jason
 
Also,just thought I'd add displaying these in a browser isn't the goal. I want to parse them so I can insert them into a database.

Just displaying to the browser to make sure I get the right data for each player before I write the script to insert it into the database.
 
Thanks fireburner!

echo "<pre>";

Worked great..now I can parse away easily.
Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top