Page 1 of 1

why this script dont work?

Posted: Sat May 25, 2002 8:42 am
by Angel
i use 2 txt files , wanna asing 1 lines of txt1 to fist line of txt 2.
place.txt is as folow:
a
b
c
destination.txt is as folow:
brussels
paris
madrid

this the code i think should work:

Code: Select all

<?php
$i= 0;
$fd = fopen ("place.txt", "r");
while (!feof ($fd)) {
    $locationї$i] = fgets($fd, 4096);
    $i++;
}
$i= 0;
fclose ($fd);
$fd = fopen ("destination.txt", "r");
while (!feof ($fd)) {
    $placeї$i] = fgets($fd, 4096);
    $i++;
}
fclose ($fd);
$result_des = array($locationї0] => $placeї0] , $locationї1] => $placeї1] ) ;
echo "The first line destination is {$result_desї"a"]}.";
?>
result should be brussels

thanx for helping

Posted: Fri May 31, 2002 3:27 am
by twigletmac
Firstly it helps if you explain what error messages you are getting and what the output is if it isn't what you want.

Ok, now try some debugging by placing the following below the "$result_des = ..." line:

Code: Select all

echo '<pre>';
print_r($result_des);
echo '</pre>';
This results in the following:

Code: Select all

Array
(
    їa
] => brussels 

    їb
] => paris 

)
As you can see there is a new line in each string so you need to remove this, alter the following lines:

Code: Select all

$locationї$i] = fgets($fd, 4096);
and
$placeї$i] = fgets($fd, 4096);
to

Code: Select all

$locationї$i] = trim(fgets($fd, 4096));
and
$placeї$i] = trim(fgets($fd, 4096));
The trim function removes the linebreaks.

Hope this helps,
Mac