How can I write greek charakters with Php into a csv-file
esp. mikro
eg:
fwrite($myFile,$key . ";" . str_replace("μ","%CE%BC",$value) .chr(13).chr(10));
My Answer:
To write Greek characters into a CSV file using PHP, you can use the following code snippet:
php
$myFile = fopen('output.csv', 'w');
$data = array(
'key1' => 'μικρό',
'key2' => 'μεγάλο',
);
foreach ($data as $key => $value) {
fwrite($myFile, $key . ';' . $value . PHP_EOL);
}
fclose($myFile);
In this code snippet, we are opening a CSV file for writing and then looping through an array of data containing Greek characters. We are then writing each key-value pair into the CSV file using the `fwrite` function. The `PHP_EOL` constant is used to add a new line after each row.
You do not need to replace the Greek characters with their HTML entities like `%CE%BC` in this case. Just directly write the Greek characters into the CSV file and they should be displayed correctly when you open the file in a text editor or spreadsheet program that supports UTF-8 encoding.
Rate this post
3 of 5 based on 9056 votesComments