How can one determine whether a string is in UTF-8 or ISO8859-1 format in PHP?

To determine whether a string is in UTF-8 or ISO8859-1 format in PHP, you can use the `mb_detect_encoding()` function. This function detects the character encoding of a string by examining its byte order mark (BOM) or analyzing the byte sequence. You can pass the string and the list of possible encodings to the function, and it will return the detected encoding.

// Check if a string is in UTF-8 or ISO8859-1 format
function detectEncoding($string) {
    $encodings = ['UTF-8', 'ISO-8859-1'];
    return mb_detect_encoding($string, $encodings);
}

// Example of usage
$string = "Hello, 你好";
$encoding = detectEncoding($string);
echo "Detected encoding: " . $encoding;