How can the chop() function in PHP be used to handle newline characters when generating CSV files for Excel?

When generating CSV files for Excel in PHP, newline characters can cause issues with the formatting of the file. To handle newline characters, you can use the `chop()` function to remove any trailing whitespace, including newline characters, from each line before writing it to the CSV file. This ensures that the data is properly formatted and displayed correctly in Excel.

$csvData = "Name,Email,Phone\nJohn Doe,johndoe@example.com,555-1234\nJane Smith,janesmith@example.com,555-5678\n";

$lines = explode("\n", $csvData);

$fp = fopen('output.csv', 'w');

foreach ($lines as $line) {
    $cleanLine = chop($line);
    fputcsv($fp, str_getcsv($cleanLine));
}

fclose($fp);