After hours of searching, I can't find a solution for saving a file in a forced UTF-8 encoding. If there is any character in a string which is only available in UTF-8, the file is successfully saved as a UTF-8, but if there are characters which are available in ASCII and UTF-8, the file is saved as ASCII
file_put_contents("test1.xml", "test"); // Saved as ASCII
file_put_contents("test2.xml", "test&"); // Saved as ASCII
file_put_contents("test3.xml", "tëst&"); // Saved as UTF-8
I can add a BOM to force a UTF-8 file, but the receiver of the document does not accept a BOM:
file_put_contents("utf8-force.xml", "\xEF\xBB\xBFtest&"); // Stored as UTF-8 because of the BOM
I did check the encoding with a simple code:
exec('file -I '.$file, $output);
print_r($output);
Since the character & is a single byte in ASCII and a two-byte character is UTF-8, the receiver of the file can't read the file. Is there a solution to force a file to UTF-8 without a BOM in PHP?