You are here

function _image_captcha_utf8_split in CAPTCHA 5.3

Same name and namespace in other branches
  1. 8 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

3 calls to _image_captcha_utf8_split()
image_captcha_captcha in image_captcha/image_captcha.module
Implementation of hook_captcha().
_image_captcha_generate_image in image_captcha/image_captcha.user.inc
base function for generating a image CAPTCHA
_image_captcha_image_generator_print_string in image_captcha/image_captcha.user.inc
Helper function for drawing text on the image

File

image_captcha/image_captcha.module, line 119
Implementation of image CAPTCHA for use with the CAPTCHA module

Code

function _image_captcha_utf8_split($str) {
  $characters = array();
  $len = strlen($str);
  for ($i = 0; $i < $len;) {
    $chr = ord($str[$i]);
    if (($chr & 0x80) == 0x0) {

      // one byte character (0zzzzzzz)
      $width = 1;
    }
    else {
      if (($chr & 0xe0) == 0xc0) {

        // two byte character (first byte: 110yyyyy)
        $width = 2;
      }
      elseif (($chr & 0xf0) == 0xe0) {

        // three byte character (first byte: 1110xxxx)
        $width = 3;
      }
      elseif (($chr & 0xf8) == 0xf0) {

        // four byte character (first byte: 11110www)
        $width = 4;
      }
      else {
        watchdog('CAPTCHA', t('Encountered an illegal byte while splitting an utf8 string in characters.'), WATCHDOG_ERROR);
        return $characters;
      }
    }
    $characters[] = substr($str, $i, $width);
    $i += $width;
  }
  return $characters;
}