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

x
  • 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

5312 TERRY AVE. STE. R 2250 GREYHILL ROAD

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
2250 GREYHILL ROAD

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.
0
neo_Elder
Asked:
neo_Elder
  • 4
  • 4
1 Solution
 
aldanchCommented:
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

0
 
neo_ElderAuthor Commented:
But I need to preserve the white space between the addresses when reading the file.
0
 
aldanchCommented:
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>";
var_dump($line);
echo "</pre>";

Open in new window

0
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?
0
 
aldanchCommented:
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

0
 
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!
0
 
aldanchCommented:
LOL I know the feeling. Go home! Also, go outside, the graphics are amazing! :P
0
 
neo_ElderAuthor Commented:
I know! The Sun is out! It's like HDTV, in fact it's even better then 1080P!!



0

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