You are here

function _text_captcha_utf8_split in CAPTCHA Pack 7

Same name and namespace in other branches
  1. 8 text_captcha/text_captcha.module \_text_captcha_utf8_split()
  2. 5 text_captcha/text_captcha.inc \_text_captcha_utf8_split()
  3. 6 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/lost_character_captcha/lost_character_captcha.module
Implements hook_captcha().
lost_character_process in text_captcha/lost_character_captcha/lost_character_captcha.module
Process the response before validation.
_text_captcha_word_pool_validate_word_length in text_captcha/text_captcha.inc

File

text_captcha/text_captcha.inc, line 17

Code

function _text_captcha_utf8_split($str) {
  $characters = array();
  $len = drupal_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[] = drupal_substr($str, $i, $width);
    $i += $width;
  }
  return $characters;
}