When using the following code:
$myString = 'some contents';
$fh=fopen('newfile.txt',"w");
fwrite($fh, "\xEF\xBB\xBF" . $myString);
Is there any point of using PHP functions to first encode the text ($myString in the example) e.g. like running utf8_encode($myString);
or similar iconv()
commands?
Assuming that the BOM \xEF\xBB\xBF
is first inputted into the file and that UTF8 represents practically all characters in the world I don't see any potential failure scenarion of creating a file this way. In other words I don't see any case where any major text editor wouldn't be able to interpret the newly created file corectly, displaying all characters as intended. This even if $myString
would be a PHP $_POST
variable from a HTML form. Am I right?
If your source file is UTF-8
encoded, then the string $myString
is also UTF-8
encoded, you don't need to convert it. Otherwise, you need to use iconv()
to convert the encoding first before write it to the file.
And note utf8_encode()
is used to encode an ISO-8859-1 string to UTF-8.