PHP fgets trimming white space between elements.

Posted on 2009-04-16
Last Modified: 2012-05-06
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.
Question by:neo_Elder
    LVL 8

    Expert Comment

    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


    Author Comment

    But I need to preserve the white space between the addresses when reading the file.
    LVL 8

    Expert Comment

    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


    Author Comment

    You are correct.

    Any thoughts on why substr would not be ignoring the white space?
    LVL 8

    Accepted Solution

    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


    Author Closing Comment

    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!
    LVL 8

    Expert Comment

    LOL I know the feeling. Go home! Also, go outside, the graphics are amazing! :P

    Author Comment

    I know! The Sun is out! It's like HDTV, in fact it's even better then 1080P!!


    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Suggested Solutions

    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now