file_get_contents() Breaks Up UTF-8 Characters
The file_get_contents() function in PHP is used to read the contents of a file into a string. However, it has been known to cause issues when reading files that contain UTF-8 characters. This is because the function reads the file as binary data and doesn't automatically handle the UTF-8 encoding. To fix this issue, you can use the utf8_encode()
function to convert the binary data to a UTF-8 encoded string before using it.
Alternatively, you can use mb_convert_encoding()
to convert the file contents to the desired encoding.
<?php
$contents = file_get_contents('file.txt');
echo $contents = utf8_encode($contents);
Watch a video course
Learn object oriented PHP
or
<?php
$contents = file_get_contents('file.txt');
echo $contents = mb_convert_encoding($contents, 'UTF-8', 'auto');