Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 968
  • Last Modified:

PHP fgets trimming white space between elements.

I have a text file I am processing line by line using fgets.

The text file has lines that appear in a text editor vi, notepad, etc. with several spaces between elements.

If I read the line using fgets the spaces get trimmed or condensed or something I'm not sure.

For instance I have a line from a text file:

5312 TERRY AVE. STE. R                 2250 GREYHILL ROAD

If I read it with fgets:   $line= fgets($fh);
and then var_dump($line);
it displays as


What's more interesting, I am using substr to get the elements I need.

$address1 = substr($line,0,30);
$address2 = substr($line, 40,30);

Now if I echo $address1 I get
5312 TERRY AVE. STE. R 2250 GR

Which is wrong because it is including the first couple letters of address 2

And if I echo address 2 I get

I'm not sure why fgets is condensing the white space. Plus it doesn't make sense how address2 starts at the correct location column 40 while address 1 should be columns 0-29 but includes what is apparently column 40,41,42

I hope that makes sense, anyhow any help is much appreciated.
  • 4
  • 4
1 Solution
You could try stripping it before reading it.
$line= fgets($fh);
$line = php_strip_whitespace($line);
or maybe...
$line = preg_replace('/\s\s+/', ' ', $line);

Open in new window

neo_ElderAuthor Commented:
But I need to preserve the white space between the addresses when reading the file.
Your var_dump is probably not accurate. If it is displayed as HTML it wouldn't matter how many spaces you have... it would condense it.

try this.
echo "<pre>";
echo "</pre>";

Open in new window

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

neo_ElderAuthor Commented:
You are correct.

Any thoughts on why substr would not be ignoring the white space?
Yeah that is very strange. When I try to re-create it everything works like it should. Are you sure those are actually spaces?

I wonder what the ASCII value of one of those white space characters is?

$line = "5312 TERRY AVE. STE. R                 2250 GREYHILL ROAD";
echo substr($line, 0, 30) ."<br/><br/>";
// This should be '32' for a regular space
echo ord(substr($line, 30, 1));

Open in new window

neo_ElderAuthor Commented:
It's time to go home, I just realized my mistake. Thanks for your help.....

My mistake was that address 1 actually starts on column 5 and address two starts on column 40, and I wasn't subtracting 5 from the length of address 1 therefor substr is doing what I told it to and is grabbing columns 40,41,42,..... etc.  AMAZING I change the length to 35 and it does what it's suppose to!!

UGH apparently 10 hours in front of the keyboard is too much.   Thanks for your help!
LOL I know the feeling. Go home! Also, go outside, the graphics are amazing! :P
neo_ElderAuthor Commented:
I know! The Sun is out! It's like HDTV, in fact it's even better then 1080P!!


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now