/* * boolean utf8_validate( string $s ) : * * Check that $s string is a valid UTF-8 string. * Return TRUE on success, FALSE on error. * It does NOT check for overlong UTF-8 sequences ! * * Copyright 2007 (c) LiNuCe, GCoiNCoin Inc LTD Gmbh * Under GPL Licence v2 or later. */ function utf8_validate( $s ) { $iLen = strlen( $s ); for( $i=0 ; $i<$iLen ; $i++ ) { $iByte = ord( $s[$i] ); if( $iByte < 128 ) continue ; if ( ( $iByte & 0xC0 ) == 0xC0 ) $iCharLen = 2 ; elseif( ( $iByte & 0xE0 ) == 0xE0 ) $iCharLen = 3 ; elseif( ( $iByte & 0xF0 ) == 0xF0 ) $iCharLen = 4 ; else return FALSE ; $iCharLen -- ; while( $iCharLen ) { if( ( ord($s[$i]) & 0x80 ) != 0x80 ) return FALSE ; $iCharLen -- ; $i++ ; } } return TRUE ; }