You are here

function _image_captcha_utf8_split in CAPTCHA 8

Same name and namespace in other branches
  1. 5.3 image_captcha/image_captcha.module \_image_captcha_utf8_split()
  2. 6.2 image_captcha/image_captcha.module \_image_captcha_utf8_split()
  3. 6 image_captcha/image_captcha.module \_image_captcha_utf8_split()
  4. 7 image_captcha/image_captcha.module \_image_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.

Parameters

string $str: UTF8 string to be split.

Return value

array List of caracters given string consists of.

4 calls to _image_captcha_utf8_split()
CaptchaImageRefresh::refreshCaptcha in image_captcha/src/Controller/CaptchaImageRefresh.php
Put your code here.
CaptchaImageResponse::printString in image_captcha/src/Response/CaptchaImageResponse.php
Helper function for drawing text on the image.
image_captcha_captcha in image_captcha/image_captcha.module
Implements hook_captcha().
_image_captcha_image_size in image_captcha/image_captcha.module
Helper function for calculating image height and width.

File

image_captcha/image_captcha.module, line 143
Implements image CAPTCHA for use with the CAPTCHA module.

Code

function _image_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;
}