What are some best practices for handling encoding issues, such as BOM, when converting CSV files from ANSI to UTF-8 in PHP?

When converting CSV files from ANSI to UTF-8 in PHP, it's important to handle encoding issues such as the Byte Order Mark (BOM) correctly. One way to do this is by detecting and removing the BOM before converting the file to UTF-8. This can be done using PHP's `file_get_contents()` function to read the file contents, checking for the presence of the BOM, and then using `mb_convert_encoding()` to convert the file to UTF-8 without the BOM.

$file = 'example.csv';

// Read file contents and remove BOM if present
$content = file_get_contents($file);
if (substr($content, 0, 3) == pack("CCC", 0xef, 0xbb, 0xbf)) {
    $content = substr($content, 3);
}

// Convert file contents to UTF-8
$content = mb_convert_encoding($content, 'UTF-8', 'Windows-1252');

// Save the converted content back to the file
file_put_contents($file, $content);