UTF8 string length counter

The best way to determine the character count of a UTF8 string.

function strlen_utf8( $str )
{
   return mb_strlen( $str, 'UTF-8' );
}
 
//or...
function strlen_utf8( $str )
{
   $c = 0;
   $len = strlen( $str );
   for ( $i = 0; $i < $len; $i++ ) {
      //0x80 = 128
      $ord = ord( $str[$i] );
      if ( $ord & 0x80 ) {
         $ord <<= 1;
         while ( $ord & 0x80 ) {
            $i++;
            $ord <<= 1;
         }
 
         $c++;
      }
      else {
         $c++;
      }
   }
 
   return $c;
}

Tags

No tag here.

Recommended pages

Detect leap year...

Detect leap year for gregorian and shamsi date. function gLeapYear($year) { if (($year % 4 == 0) and (($year % 100 != 0) or ($year % 400 == 0))) return true; else return ...

Validate phone number...

Chek if a phone numbers is valid, according to its syntax (should be: "+390523599314"). True if it's valid, False otherwise function is_valid_phone( $phone ) { if ( $phone[0] != "+" ) { ...

Create directory function...

Create directory if not exists. function createDir( $dir ) { if ( !is_dir( $dir ) ) { mkdir( $dir, 0777 ); return true; } return false; }...

Detect file extension function...

This function is used to find the file extension also you can use pathinfo() function for get information about a file path. function ext($file_name) { return substr($file_name, strrpos($fi...