What are the best practices for detecting and handling encoding issues in PHP when working with external data sources?

When working with external data sources in PHP, it is important to handle encoding issues to prevent unexpected behavior or errors. One common approach is to use functions like mb_detect_encoding() to determine the encoding of the incoming data and then convert it to the desired encoding using mb_convert_encoding(). Additionally, setting the default_charset in your PHP configuration can help ensure consistent handling of encoding throughout your application.

// Detect and convert encoding of incoming data
$incomingData = "“Hello, World!�";
$detectedEncoding = mb_detect_encoding($incomingData, mb_detect_order(), true);
$convertedData = mb_convert_encoding($incomingData, 'UTF-8', $detectedEncoding);

// Set default_charset in PHP configuration
ini_set('default_charset', 'UTF-8');