How can I write a file in UTF-8 format?
To write a file in UTF-8 encoding with PHP, you can use the fopen
, fwrite
, and fclose
functions. Here is an example of how you can use these functions to write a string to a file in UTF-8 encoding:
<?php
$str = "Hello, world!";
$file = fopen("output.txt", "w");
fwrite($file, $str);
fclose($file);
This will create a new file called "output.txt" and write the string "Hello, world!" to it. The file will be saved in UTF-8 encoding.
If you want to specify the encoding when you create the file, you can use the utf8_encode
function to convert the string to UTF-8 encoding before writing it to the file. Here is an example:
<?php
$str = "Hello, world!";
$file = fopen("output.txt", "w");
fwrite($file, utf8_encode($str));
fclose($file);
This will create a new file called "output.txt" and write the string "Hello, world!" to it, using UTF-8 encoding.