seems_utf8

Checks to see if a string is utf8 encoded.

Description

seems_utf8( (string) $str ); 

NOTE: This function checks for 5-Byte sequences, UTF8 has Bytes Sequences with a maximum length of 4.

Parameters (1)

0. $str (string)
The string to be checked

Usage

  1. if ( !function_exists( 'seems_utf8' ) ) { 
  2. require_once ABSPATH . WPINC . '/formatting.php'; 
  3.  
  4. // The string to be checked 
  5. $str = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = seems_utf8($str); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/formatting.php  
  1. function seems_utf8( $str ) { 
  2. $length = strlen($str); 
  3. for ($i=0; $i < $length; $i++) { 
  4. $c = ord($str[$i]); 
  5. if ($c < 0x80) $n = 0; // 0bbbbbbb 
  6. elseif (($c & 0xE0) == 0xC0) $n=1; // 110bbbbb 
  7. elseif (($c & 0xF0) == 0xE0) $n=2; // 1110bbbb 
  8. elseif (($c & 0xF8) == 0xF0) $n=3; // 11110bbb 
  9. elseif (($c & 0xFC) == 0xF8) $n=4; // 111110bb 
  10. elseif (($c & 0xFE) == 0xFC) $n=5; // 1111110b 
  11. else return false; // Does not match any model 
  12. for ($j=0; $j<$n; $j++) { // n bytes matching 10bbbbbb follow ? 
  13. if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80)) 
  14. return false; 
  15. return true;