...

View Full Version : Why would a , break down a string into individual lines?



MrBiggZ
07-03-2010, 03:32 AM
Hi!

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


"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
/***********************************************************************/

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?? :confused: 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:


"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! :thumbsup:

Fou-Lu
07-03-2010, 08:37 AM
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.

MrBiggZ
07-03-2010, 04:12 PM
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:

"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. :eek:

Fou-Lu
07-03-2010, 05:22 PM
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).

MrBiggZ
07-03-2010, 06:44 PM
Thanks Fou ..

Here's what the original data looks like:


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:

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! :confused:

Fou-Lu
07-03-2010, 09:18 PM
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.

MrBiggZ
07-04-2010, 12:13 AM
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! :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum