You are here

public static function UTF8Utils::countChars in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/masterminds/html5/src/HTML5/Parser/UTF8Utils.php \Masterminds\HTML5\Parser\UTF8Utils::countChars()

Count the number of characters in a string.

UTF-8 aware. This will try (in order) iconv, MB, libxml, and finally a custom counter.

@todo Move this to a general utility class.

1 call to UTF8Utils::countChars()
StringInputStream::columnOffset in vendor/masterminds/html5/src/HTML5/Parser/StringInputStream.php
Returns the current column of the current line that the tokenizer is at.

File

vendor/masterminds/html5/src/HTML5/Parser/UTF8Utils.php, line 48

Class

UTF8Utils
UTF-8 Utilities

Namespace

Masterminds\HTML5\Parser

Code

public static function countChars($string) {

  // Get the length for the string we need.
  if (function_exists('iconv_strlen')) {
    return iconv_strlen($string, 'utf-8');
  }
  elseif (function_exists('mb_strlen')) {
    return mb_strlen($string, 'utf-8');
  }
  elseif (function_exists('utf8_decode')) {

    // MPB: Will this work? Won't certain decodes lead to two chars
    // extrapolated out of 2-byte chars?
    return strlen(utf8_decode($string));
  }
  $count = count_chars($string);

  // 0x80 = 0x7F - 0 + 1 (one added to get inclusive range)
  // 0x33 = 0xF4 - 0x2C + 1 (one added to get inclusive range)
  return array_sum(array_slice($count, 0, 0x80)) + array_sum(array_slice($count, 0xc2, 0x33));
}