Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder MrBiggZ's Avatar
    Join Date
    Apr 2005
    Location
    Indianapolis IN
    Posts
    345
    Thanks
    44
    Thanked 2 Times in 2 Posts

    Question Why would a , break down a string into individual lines?

    Hi!

    I'm writing to a file and it seems the comma are breaking down the string. This is the output I'm getting:

    Code:
    "Kroger , 524 E 16th St , 46202 IN>(317) 923-6858"
    "Kroger , 680 Twin Aire Dr , 46203 IN>(317) 264-1762"
    "Kroger , 2630 W Michigan St , 46222 IN>(317) 632-2113"
    "Kroger , 4445 E 10th St , 46201 IN>(317) 359-1166"
    
    Array
    (
        [0] => "Kroger
     , 524 E 16th St
     , 46202 IN>(317) 923-6858"
        [1] => "Kroger
     , 680 Twin Aire Dr
     , 46203 IN>(317) 264-1762"
        [2] => "Kroger
     , 2630 W Michigan St
     , 46222 IN>(317) 632-2113"
        [3] => "Kroger
     , 4445 E 10th St
     , 46201 IN>(317) 359-1166"
    )
    This is my script:
    PHP Code:
    <?php
    /***********************************************************************/

    ini_set('display_errors'1);
    ini_set('error_reporting'E_ALL);
    ini_set('display_startup_errors'1);

    /***********************************************************************/

    $fh file("kroger.txt");

    $krog1 fopen("kroger_GPS01.txt","w");

    foreach (
    $fh as $key=>$value) {
        if (
    trim($value) == "KROGER") { 

        
    $zip preg_split("/\D/",$fh[$key 2]);
            for (
    $index=0;$index<count($zip);$index++)
                {
                    if (
    strlen($zip[$index]) > 0) {
                        
    $GPSzip  $zip[$index];
                    }
                }

        
    $state preg_split("/ /",$fh[$key 2]);
            for (
    $index=0;$index<count($state);$index++)
                {
                    if ((
    $state[$index]) == "IN") {
                        
    $GPSstate $state[$index];
                    }
                }

        
    $phone preg_split("/\D+/",$fh[$key 3]);
        
    $phoneFmt "(" $phone[1] . ") " $phone[2] . "-" $phone[3];
        echo 
    "\"" ucwords(strtolower($value)) . " , " $fh[$key 1] . " , " $GPSzip " " $GPSstate ">" $phoneFmt "\"<br>\n";
        
    $line[] = "\"" ucwords(strtolower($value)) . " , " $fh[$key 1] . " , " $GPSzip " " $GPSstate ">" $phoneFmt "\"";
        }
    }
    for (
    $index=0;$index<count($line);$index++) {
        
    fwrite($krog1,$line[$index]);
        }
    fclose($krog1);


    echo 
    "<pre>";
    echo 
    print_r($line);
    echo 
    "</pre>";
    Why would a comma break down the string like that?? When I echo it to the screen it one line per record and in the file 3 lines per record and the beginning of the next line start on the end of the last record. Better if I show you:

    Code:
    "Kroger
     , 524 E 16th St
     , 46202 IN>(317) 923-6858""Kroger
     , 680 Twin Aire Dr
     , 46203 IN>(317) 264-1762""Kroger
     , 2630 W Michigan St
     , 46222 IN>(317) 632-2113""Kroger
     , 4445 E 10th St
     , 46201 IN>(317) 359-1166"
    Any help is greatly appreciated! Thx!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Put in an var_dump($fh); and post the result. Wrap it in pre tags or take it from the source. I'll bet there are many \n array elements.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder MrBiggZ's Avatar
    Join Date
    Apr 2005
    Location
    Indianapolis IN
    Posts
    345
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Fou-Lu View Post
    Put in an var_dump($fh); and post the result. Wrap it in pre tags or take it from the source. I'll bet there are many \n array elements.
    I actually used $line instead of $fh. I then wrapped each of the items in a trim statement

    $line[] = "\"" . ucwords(strtolower(trim($value))) . " , " . trim($fh[$key + 1]) . " , " . trim($GPSzip) . " " . trim($GPSstate) . ">" . trim($phoneFmt) . "\"";

    My output file now looks like this:
    Code:
    "Kroger , 524 E 16th St , 46202 IN>(317) 923-6858""Kroger , 680 Twin Aire Dr , 46203 IN>(317) 264-1762""Kroger , 2630 W Michigan St , 46222 IN>(317) 632-2113""Kroger , 4445 E 10th St , 46201 IN>(317) 359-1166"
    I tried putting a \n at the end of the $line[] and then tried \r which yielded the same results.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Each line written needs a new line, when you added \n did you put it at the end of each string, or was it appended as an item? You should just write the string; I see no reason to put it in an array first What does this original data look like? Given the jumps in array locations, it definetly can't be the same as the output.
    In any case though, each item can be trimmed by file() if you give a second argument of FILE_IGNORE_NEW_LINES (var_dump($fh) would allow you to detect these linefeeds as the string would show split and the length won't add up).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    Regular Coder MrBiggZ's Avatar
    Join Date
    Apr 2005
    Location
    Indianapolis IN
    Posts
    345
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Thanks Fou ..

    Here's what the original data looks like:

    Code:
    KROGER
    524 E 16th St
    Indianapolis, IN 46202
    Store: (317) 923-6858
    Distance
    1.5 miles
    Store Details
    Make Preferred Store
    Store Directions
    View Weekly Ad
    KROGER
    680 Twin Aire Dr
    Indianapolis, IN 46203
    Store: (317) 264-1762
    Pharmacy: (317) 264-1755
    Distance
    1.9 miles
    Store Details
    Make Preferred Store
    Store Directions
    View Weekly Ad
    Wellness Services
    KROGER
    2630 W Michigan St
    Indianapolis, IN 46222
    Store: (317) 632-2113
    Distance
    2.9 miles
    Store Details
    Make Preferred Store
    Store Directions
    View Weekly Ad
    KROGER
    4445 E 10th St
    Indianapolis, IN 46201
    Store: (317) 359-1166
    Pharmacy: (317) 359-1638
    Distance
    3.1 miles
    Store Details
    Make Preferred Store
    Store Directions
    View Weekly Ad
    Wellness Services
    The dump_var of $fh gives this:
    Code:
    array(44) {
      [0]=>
      string(8) "KROGER
    "
      [1]=>
      string(15) "524 E 16th St
    "
      [2]=>
      string(24) "Indianapolis, IN 46202
    "
      [3]=>
      string(23) "Store: (317) 923-6858
    "
      [4]=>
      string(10) "Distance
    "
      [5]=>
      string(11) "1.5 miles
    "
      [6]=>
      string(15) "Store Details
    "
      [7]=>
      string(22) "Make Preferred Store
    "
      [8]=>
      string(18) "Store Directions
    "
      [9]=>
      string(16) "View Weekly Ad
    "
      [10]=>
      string(8) "KROGER
    "
      [11]=>
      string(18) "680 Twin Aire Dr
    "
      [12]=>
      string(24) "Indianapolis, IN 46203
    "
      [13]=>
      string(23) "Store: (317) 264-1762
    "
      [14]=>
      string(26) "Pharmacy: (317) 264-1755
    "
      [15]=>
      string(10) "Distance
    "
      [16]=>
      string(11) "1.9 miles
    "
      [17]=>
      string(15) "Store Details
    "
      [18]=>
      string(22) "Make Preferred Store
    "
      [19]=>
      string(18) "Store Directions
    "
      [20]=>
      string(16) "View Weekly Ad
    "
      [21]=>
      string(19) "Wellness Services
    "
      [22]=>
      string(8) "KROGER
    "
      [23]=>
      string(20) "2630 W Michigan St
    "
      [24]=>
      string(24) "Indianapolis, IN 46222
    "
      [25]=>
      string(23) "Store: (317) 632-2113
    "
      [26]=>
      string(10) "Distance
    "
      [27]=>
      string(11) "2.9 miles
    "
      [28]=>
      string(15) "Store Details
    "
      [29]=>
      string(22) "Make Preferred Store
    "
      [30]=>
      string(18) "Store Directions
    "
      [31]=>
      string(16) "View Weekly Ad
    "
      [32]=>
      string(8) "KROGER
    "
      [33]=>
      string(16) "4445 E 10th St
    "
      [34]=>
      string(24) "Indianapolis, IN 46201
    "
      [35]=>
      string(23) "Store: (317) 359-1166
    "
      [36]=>
      string(26) "Pharmacy: (317) 359-1638
    "
      [37]=>
      string(10) "Distance
    "
      [38]=>
      string(11) "3.1 miles
    "
      [39]=>
      string(15) "Store Details
    "
      [40]=>
      string(22) "Make Preferred Store
    "
      [41]=>
      string(18) "Store Directions
    "
      [42]=>
      string(16) "View Weekly Ad
    "
      [43]=>
      string(19) "Wellness Services
    "
    }
    Please bare with me Fou .. I'm green at this. COBOL I can do .. this is an animal of another color!

    I did try sticking a \n at the end of that line. Now whats funny is opening it in windows notepad it shows as one long line. If I open it in Notepad++ everything looks as if it should!

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Use the constant PHP_EOL instead. Linefeeds are always a pain when used on different platforms.
    Just use FILE_IGNORE_NEW_LINES in file, it will save you the trim.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    MrBiggZ (07-04-2010)

  • #7
    Regular Coder MrBiggZ's Avatar
    Join Date
    Apr 2005
    Location
    Indianapolis IN
    Posts
    345
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Fou-Lu View Post
    Use the constant PHP_EOL instead. Linefeeds are always a pain when used on different platforms.
    Just use FILE_IGNORE_NEW_LINES in file, it will save you the trim.
    Cool Thanks! I learned a bit here!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •