Snippet. PHP. Check if String is in UTF-8 Encoding

Many times you need to see if a string is UTF-8 encoded in order to correctly work with it. This little function will check if a string is in UTF-8 encoding and will return the result.

Function str_is_utf8

//======================== START OF FUNCTION ==========================//
// FUNCTION: str_is_utf8                                               //
//=====================================================================//
   /**
    * Checks a string for UTF-8 encoding.
    *
    * @param string $string
    * @return boolean true if sring is UTF-8 encoded, false otherwise
    */
function str_is_utf8($string) {
    $length = strlen($string);

    for ($i = 0; $i < $length; $i++) {
        if (ord($string[$i]) < 0x80) {
            $n = 0;
        } else if ((ord($string[$i]) & 0xE0) == 0xC0) { 
            $n = 1;
        } else if ((ord($string[$i]) & 0xF0) == 0xE0) {
            $n = 2;
        } else if ((ord($string[$i]) & 0xF0) == 0xF0) {
            $n = 3;
        } else {
             return false;
        }

        for ($j = 0; $j < $n; $j++) {
            if ((++$i == $length) || ((ord($string[$i]) & 0xC0) != 0x80)) {
                return false;
            }
        }
    }

    return false;
}
//=====================================================================//
//  FUNCTION: str_is_utf8                                              //
//========================= END OF FUNCTION ===========================//

Updated on: 20 Apr 2024