I having a problem getting my file to look like this

@email1.com
@email2.com
@email3.com

Instead, when i write the contents of an array to a file it looks like this.

@email1.com@email2.com@email3.com

When i gather the information during processing i do alot of trimming so i would need to be able to add back /n before writing. I'v tried various methods including the explode method but all i get are errors.

Here is an example of a routine where i would need to write to a file and add /n after every record is written.

    $file1 = file ($datafile);
    $file2 = array_map ('trim', $file1);
    $file3 = array_unique ($file2);
    $file00 = fopen ($datafile, 'w');
    foreach ($file3 as $line)
    {
      fwrite ($file00, $line . '');

I did try;

fwrite ($file00, $line . "\n"); but this didnt work.

    You could try something along these lines:

    $lines = file($datafile);
    $lines = array_map('trim', $lines);
    $lines = array_unique($lines);
    $data = implode("\n", $lines) . "\n";
    file_put_contents($datafile, $data);

      Im not sure i really understand that. Cant i just add something to fwrite line as i have it? Since i have 4 instances of fwrite i would like to keep it pretty much untouched. The impode gives me an error. Is there anything else i can do?

        Im not sure i really understand that.

        The idea is to join back all the lines into a single string with "\n" as the "glue". However, [man]implode/man does not add the glue string to the end, so this is appended manually.

        // Read the lines of $datafile into an array named $lines.
        $lines = file($datafile);
        // Trim whitespace from each line (i.e., array element).
        $lines = array_map('trim', $lines);
        // Remove duplicate lines.
        $lines = array_unique($lines);
        // Join the lines, separated by "\n", into a single string.
        $data = implode("\n", $lines) . "\n";
        // Write the string into $datafile.
        file_put_contents($datafile, $data);

        Cant i just add something to fwrite line as i have it?

        Yes. As in your other thread, you would change the string $line . '' to $line . "\n".

        Since i have 4 instances of fwrite i would like to keep it pretty much untouched.

        It sounds like you should be generalising this code into a function.

        The impode gives me an error.

        What is the error message? Just saying it gives you an error does not help anyone else diagnose the problem and propose a solution.

          Write a Reply...