How can special characters like semicolons be handled in CSV files in PHP?
Special characters like semicolons can be handled in CSV files in PHP by enclosing the fields containing special characters within double quotes. This ensures that the semicolons are treated as part of the field value and not as delimiters. Additionally, special characters can be escaped using the backslash (\) character to prevent any parsing issues.
$data = array(
array('Name', 'Age', 'Email'),
array('John Doe', 25, 'john.doe@example.com'),
array('Jane Smith', 30, 'jane.smith@example.com;extra'),
);
$fp = fopen('data.csv', 'w');
foreach ($data as $fields) {
fputcsv($fp, $fields, ';', '"');
}
fclose($fp);
Keywords
Related Questions
- What are the different visibility levels for variables in PHP classes and how does it affect access from other classes?
- How can PHP be used to read files from different directories on a Windows server?
- In the provided PHP script, what improvements can be made to optimize the code for better readability and efficiency, especially for handling large amounts of data from a MySQL database?