How can one determine the encoding of a string in PHP when working with FPDF?

When working with FPDF in PHP, you may encounter issues with encoding if your string contains characters that are not supported by the default encoding. To determine the encoding of a string, you can use the mb_detect_encoding function in PHP. This function will return the encoding of the string, which you can then use to properly set the encoding in FPDF.

// Sample string with unknown encoding
$text = "Hello, 你好, مرحبا";

// Detect the encoding of the string
$encoding = mb_detect_encoding($text);

// Set the encoding in FPDF
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', '', 12);
$pdf->Cell(0, 10, iconv($encoding, 'UTF-8', $text));
$pdf->Output();