Skip to content

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. It reads raw bytes correctly, so files already encoded in UTF-8 are handled without issues. However, problems can occur if the source file uses a different character encoding (such as ISO-8859-1 or Windows-1252) while your application expects UTF-8. To resolve this, you can convert the file contents to UTF-8 after reading.

Example of converting file contents to UTF-8 in PHP

php
<?php

$contents = file_get_contents('file.txt');
$encoding = mb_detect_encoding($contents, mb_detect_order(), true);
$contents = mb_convert_encoding($contents, 'UTF-8', $encoding);
echo $contents;

<div class="alert alert-info flex not-prose"> Watch a course Learn object oriented PHP</div>

or

Example of specifying source encoding in PHP

php
<?php

$contents = file_get_contents('file.txt');
$contents = mb_convert_encoding($contents, 'UTF-8', 'ISO-8859-1');
echo $contents;

Dual-run preview — compare with live Symfony routes.