Gianni Bordone
Gianni Bordone

Reputation: 11

strange behaviour with php function and save in new txt in two line

I have this file disponib.txt with work schedule:

robert=1-am|3-pm|12-am
jane=2-pm|14-am|7-pm|9-pm|1-pm
leonard=1-am|14-pm
frank=1-am

and this is my code:

<?PHP
    $FileElenco = file("disponib.txt");
    for ($i=0; $i<count($FileElenco); $i++){
        $spezza_1 = explode("=",$FileElenco[$i]);
        $name = $spezza_1[0];        //es: robert
        $disponib = $spezza_1[1];        //es: 1-am|3-pm|12-am

        $disponib_1 = explode("|",$disponib);
        for ($x=0; $x<count($disponib_1); $x++){
            $spezza_2 = explode("-",$disponib_1[$x]);
            $day = (int)$spezza_2[0];        //es: 1
            $when = $spezza_2[1];            //es: am
            $ins_name = $name."(".$when.")";        //insert name with availability
            $fileout = "Days/g_".$day.".txt";
            $out = fopen($fileout,"a") or die("Impossible open the file!!");
            fwrite($out,$ins_name."<br>") or die("Impossible write on file!!");
            fclose($out) or die("Impossible close the file!!");
        }
    }
?>

But, when save, I find the file txt like this (g_1.txt):

robert(am
)<br>jane(pm)<br>leonard(am)<br>frank(am)

and not in one row like this:

robert(am)<br>jane(pm)<br>leonard(am)<br>frank(am)

Could you help me?

Upvotes: 1

Views: 57

Answers (1)

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41820

The new lines are coming from your original file.

By default, file will include the new line characters from the input file in each array entry it creates, but you can use the FILE_IGNORE_NEW_LINES option to avoid that. This should fix it:

$FileElenco = file("disponib.txt", FILE_IGNORE_NEW_LINES);

Upvotes: 2

Related Questions