You are here

function _text_captcha_utf8_split in CAPTCHA Pack 8

Same name and namespace in other branches
  1. 5 text_captcha/text_captcha.inc \_text_captcha_utf8_split()
  2. 6 text_captcha/text_captcha.inc \_text_captcha_utf8_split()
  3. 7 text_captcha/text_captcha.inc \_text_captcha_utf8_split()

Helper function for splitting an utf8 string correctly in characters.

Assumes the given utf8 string is well formed. See http://en.wikipedia.org/wiki/Utf8 for more info.

4 calls to _text_captcha_utf8_split()
css_captcha_captcha in css_captcha/css_captcha.module
Implements hook_captcha().
lost_character_captcha_captcha in text_captcha/modules/lost_character_captcha/lost_character_captcha.module
Implements hook_captcha().
lost_character_process in text_captcha/modules/lost_character_captcha/lost_character_captcha.module
Process the response before validation.
_text_captcha_word_pool_validate_word_length in text_captcha/text_captcha.module
Validates word length.

File

text_captcha/text_captcha.module, line 26
Contains general functionality for the module.

Code

function _text_captcha_utf8_split($str) {
  $characters = [];
  $len = strlen($str);
  for ($i = 0; $i < $len;) {
    $chr = ord($str[$i]);

    // One byte character (0zzzzzzz)
    if (($chr & 0x80) == 0x0) {
      $width = 1;
    }
    else {

      // Two byte character (first byte: 110yyyyy)
      if (($chr & 0xe0) == 0xc0) {
        $width = 2;
      }
      elseif (($chr & 0xf0) == 0xe0) {
        $width = 3;
      }
      elseif (($chr & 0xf8) == 0xf0) {
        $width = 4;
      }
      else {
        \Drupal::logger('CAPTCHA')
          ->error('Encountered an illegal byte while splitting an utf8 string in characters.');
        return $characters;
      }
    }
    $characters[] = substr($str, $i, $width);
    $i += $width;
  }
  return $characters;
}