本文整理汇总了PHP中UTF8::strlen方法的典型用法代码示例。如果您正苦于以下问题:PHP UTF8::strlen方法的具体用法?PHP UTF8::strlen怎么用?PHP UTF8::strlen使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UTF8
的用法示例。
在下文中一共展示了UTF8::strlen方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _str_pad
/**
* UTF8::str_pad
*
* @package Kohana
* @author Kohana Team
* @copyright (c) 2007-2012 Kohana Team
* @copyright (c) 2005 Harry Fuecks
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
*/
function _str_pad($str, $final_str_length, $pad_str = ' ', $pad_type = STR_PAD_RIGHT)
{
if (UTF8::is_ascii($str) and UTF8::is_ascii($pad_str)) {
return str_pad($str, $final_str_length, $pad_str, $pad_type);
}
$str_length = UTF8::strlen($str);
if ($final_str_length <= 0 or $final_str_length <= $str_length) {
return $str;
}
$pad_str_length = UTF8::strlen($pad_str);
$pad_length = $final_str_length - $str_length;
if ($pad_type == STR_PAD_RIGHT) {
$repeat = ceil($pad_length / $pad_str_length);
return UTF8::substr($str . str_repeat($pad_str, $repeat), 0, $final_str_length);
}
if ($pad_type == STR_PAD_LEFT) {
$repeat = ceil($pad_length / $pad_str_length);
return UTF8::substr(str_repeat($pad_str, $repeat), 0, floor($pad_length)) . $str;
}
if ($pad_type == STR_PAD_BOTH) {
$pad_length /= 2;
$pad_length_left = floor($pad_length);
$pad_length_right = ceil($pad_length);
$repeat_left = ceil($pad_length_left / $pad_str_length);
$repeat_right = ceil($pad_length_right / $pad_str_length);
$pad_left = UTF8::substr(str_repeat($pad_str, $repeat_left), 0, $pad_length_left);
$pad_right = UTF8::substr(str_repeat($pad_str, $repeat_right), 0, $pad_length_right);
return $pad_left . $str . $pad_right;
}
throw new UTF8_Exception("UTF8::str_pad: Unknown padding type (:pad_type)", [':pad_type' => $pad_type]);
}
示例2: action_ent
public function action_ent()
{
$answers = Security::xss_clean(Arr::get($_POST, 'answers', ''));
$answers = UTF8::substr($answers, 0, UTF8::strlen($answers) - 1);
$list = explode(',', $answers);
$total = 0;
$right = 0;
$points = array();
foreach ($list as $item) {
$total++;
$e = explode('.', $item);
$quest = ORM::factory('Ent_Quest', (int) $e[0]);
if ($quest->loaded()) {
$variant = ORM::factory('Quest_Variant')->where('quest_id', '=', $quest->id)->and_where('id', '=', (int) $e[1])->and_where('right', '=', '1')->find();
if ($variant->loaded()) {
$right++;
$points[] = array('quest' => $quest->id, 'right' => 1);
} else {
$points[] = array('quest' => $quest->id, 'right' => 0);
}
}
}
$data = array('total' => $total, 'right' => $right, 'points' => $points);
$this->response->body(json_encode($data));
}
示例3: snippet
public function snippet($text = '')
{
$description = '';
$chars = array('.', '!', '?', ':', '"');
if (!empty($text)) {
$text = strip_tags($text);
$arr = explode(' ', $text);
foreach ($arr as $k => $v) {
if (!empty($v)) {
$countdescription = UTF8::strlen($description);
$countword = UTF8::strlen($v);
if ($countdescription - 1 + $countword > 140) {
break;
} else {
$description .= $v . ' ';
}
}
}
$description = rtrim($description);
if (!empty($description)) {
$lastchar = $description[UTF8::strlen($description) - 1];
if ($lastchar == ',') {
$description = UTF8::substr($description, 0, UTF8::strlen($description) - 1);
}
if (!in_array($lastchar, $chars)) {
$description .= '...';
}
}
}
$this->description = $description;
return $this;
}
示例4: render
/**
* Outputs the Captcha image.
*
* @param boolean $html HTML output
* @return mixed
*/
public function render($html = TRUE)
{
// Creates a black image to start from
$this->image_create(Captcha::$config['background']);
// Add random white/gray arcs, amount depends on complexity setting
$count = (Captcha::$config['width'] + Captcha::$config['height']) / 2;
$count = $count / 5 * min(10, Captcha::$config['complexity']);
for ($i = 0; $i < $count; $i++) {
imagesetthickness($this->image, mt_rand(1, 2));
$color = imagecolorallocatealpha($this->image, 255, 255, 255, mt_rand(0, 120));
imagearc($this->image, mt_rand(-Captcha::$config['width'], Captcha::$config['width']), mt_rand(-Captcha::$config['height'], Captcha::$config['height']), mt_rand(-Captcha::$config['width'], Captcha::$config['width']), mt_rand(-Captcha::$config['height'], Captcha::$config['height']), mt_rand(0, 360), mt_rand(0, 360), $color);
}
// Use different fonts if available
$font = Captcha::$config['fontpath'] . Captcha::$config['fonts'][array_rand(Captcha::$config['fonts'])];
// Draw the character's white shadows
$size = (int) min(Captcha::$config['height'] / 2, Captcha::$config['width'] * 0.8 / UTF8::strlen($this->response));
$angle = mt_rand(-15 + UTF8::strlen($this->response), 15 - UTF8::strlen($this->response));
$x = mt_rand(1, Captcha::$config['width'] * 0.9 - $size * UTF8::strlen($this->response));
$y = (Captcha::$config['height'] - $size) / 2 + $size;
$color = imagecolorallocate($this->image, 255, 255, 255);
imagefttext($this->image, $size, $angle, $x + 1, $y + 1, $color, $font, $this->response);
// Add more shadows for lower complexities
Captcha::$config['complexity'] < 10 and imagefttext($this->image, $size, $angle, $x - 1, $y - 1, $color, $font, $this->response);
Captcha::$config['complexity'] < 8 and imagefttext($this->image, $size, $angle, $x - 2, $y + 2, $color, $font, $this->response);
Captcha::$config['complexity'] < 6 and imagefttext($this->image, $size, $angle, $x + 2, $y - 2, $color, $font, $this->response);
Captcha::$config['complexity'] < 4 and imagefttext($this->image, $size, $angle, $x + 3, $y + 3, $color, $font, $this->response);
Captcha::$config['complexity'] < 2 and imagefttext($this->image, $size, $angle, $x - 3, $y - 3, $color, $font, $this->response);
// Finally draw the foreground characters
$color = imagecolorallocate($this->image, 0, 0, 0);
imagefttext($this->image, $size, $angle, $x, $y, $color, $font, $this->response);
// Output
return $this->image_render($html);
}
示例5: _pad_word
protected function _pad_word(&$word, $letter, $length = 4)
{
$l = UTF8::strlen($word);
if ($l < $length) {
$word = str_repeat($letter, $length - $l) . $word;
}
}
示例6: _str_pad
/**
* UTF8::str_pad
*
* @package Kohana
* @author Kohana Team
* @copyright (c) 2007-2010 Kohana Team
* @copyright (c) 2005 Harry Fuecks
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
*/
function _str_pad($str, $final_str_length, $pad_str = ' ', $pad_type = STR_PAD_RIGHT)
{
if (UTF8::is_ascii($str) and UTF8::is_ascii($pad_str)) {
return str_pad($str, $final_str_length, $pad_str, $pad_type);
}
$str_length = UTF8::strlen($str);
if ($final_str_length <= 0 or $final_str_length <= $str_length) {
return $str;
}
$pad_str_length = UTF8::strlen($pad_str);
$pad_length = $final_str_length - $str_length;
if ($pad_type == STR_PAD_RIGHT) {
$repeat = ceil($pad_length / $pad_str_length);
return UTF8::substr($str . str_repeat($pad_str, $repeat), 0, $final_str_length);
}
if ($pad_type == STR_PAD_LEFT) {
$repeat = ceil($pad_length / $pad_str_length);
return UTF8::substr(str_repeat($pad_str, $repeat), 0, floor($pad_length)) . $str;
}
if ($pad_type == STR_PAD_BOTH) {
$pad_length /= 2;
$pad_length_left = floor($pad_length);
$pad_length_right = ceil($pad_length);
$repeat_left = ceil($pad_length_left / $pad_str_length);
$repeat_right = ceil($pad_length_right / $pad_str_length);
$pad_left = UTF8::substr(str_repeat($pad_str, $repeat_left), 0, $pad_length_left);
$pad_right = UTF8::substr(str_repeat($pad_str, $repeat_right), 0, $pad_length_right);
return $pad_left . $str . $pad_right;
}
trigger_error('UTF8::str_pad: Unknown padding type (' . $pad_type . ')', E_USER_ERROR);
}
示例7: _wordwrap
/**
* UTF8::wordwrap
* Taken and adapted form Zend Framework by Ivan Tcholakov, 2015.
*
* @param string The input string.
* @param int The number of characters at which the string will be wrapped.
* @param string The line is broken using the optional break parameter.
* @param bool If the cut is set to TRUE, the string is always wrapped at or before the specified width.
* @return string|false
* @license @license http://framework.zend.com/license/new-bsd New BSD License
*/
function _wordwrap($string, $width = 75, $break = "\n", $cut = false)
{
$string = @(string) $string;
if ($string === '') {
return '';
}
$break = @(string) $break;
if ($break === '') {
trigger_error('UTF8::wordwrap(): Break string cannot be empty.', E_USER_WARNING);
return false;
}
$width = (int) $width;
if ($width === 0 && $cut) {
trigger_error('UTF8::wordwrap(): Cannot force cut when width is zero.', E_USER_WARNING);
return false;
}
$string_length = UTF8::strlen($string);
$break_length = UTF8::strlen($break);
$result = '';
$last_start = 0;
$last_space = 0;
for ($current = 0; $current < $string_length; $current++) {
$char = UTF8::substr($string, $current, 1);
$possible_break = $char;
if ($break_length !== 1) {
$possible_break = UTF8::substr($string, $current, $break_length);
}
if ($possible_break === $break) {
$result .= UTF8::substr($string, $last_start, $current - $last_start + $break_length);
$current += $break_length - 1;
$last_start = $last_space = $current + 1;
continue;
}
if ($char === ' ') {
if ($current - $last_start >= $width) {
$result .= UTF8::substr($string, $last_start, $current - $last_start) . $break;
$last_start = $current + 1;
}
$last_space = $current;
continue;
}
if ($current - $last_start >= $width && $cut && $last_start >= $last_space) {
$result .= UTF8::substr($string, $last_start, $current - $last_start) . $break;
$last_start = $last_space = $current;
continue;
}
if ($current - $last_start >= $width && $last_start < $last_space) {
$result .= UTF8::substr($string, $last_start, $last_space - $last_start) . $break;
$last_start = $last_space = $last_space + 1;
continue;
}
}
if ($last_start !== $current) {
$result .= UTF8::substr($string, $last_start, $current - $last_start);
}
return $result;
}
示例8: exact_length
public function exact_length($str, $val)
{
if (!is_numeric($val)) {
return false;
} else {
$val = (int) $val;
}
return IS_UTF8_CHARSET ? UTF8::strlen($str) === $val : strlen($str) === $val;
}
示例9: clean
public function clean($replacement = '#', $replace_partial_words = FALSE)
{
$regex = $this->get_pattern($replace_partial_words);
if (UTF8::strlen($replacement) == 1) {
$regex .= 'e';
return preg_replace($regex, 'str_repeat($replacement, UTF8::strlen(\'$1\'))', $this->string);
}
return preg_replace($regex, $replacement, $this->string);
}
示例10: _substr_replace
/**
* UTF8::substr_replace
*
* @package Kohana
* @author Kohana Team
* @copyright (c) 2007-2012 Kohana Team
* @copyright (c) 2005 Harry Fuecks
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
*/
function _substr_replace($str, $replacement, $offset, $length = null)
{
if (UTF8::is_ascii($str)) {
return $length === null ? substr_replace($str, $replacement, $offset) : substr_replace($str, $replacement, $offset, $length);
}
$length = $length === null ? UTF8::strlen($str) : (int) $length;
preg_match_all('/./us', $str, $str_array);
preg_match_all('/./us', $replacement, $replacement_array);
array_splice($str_array[0], $offset, $length, $replacement_array[0]);
return implode('', $str_array[0]);
}
示例11: countThis
private function countThis($string)
{
$string = strip_tags($string);
$e = explode(" ", $string);
foreach ($e as $k => $word) {
if (UTF8::strlen($word) < 2) {
unset($e[$k]);
}
}
return count($e);
}
示例12: _substr
/**
* UTF8::substr
*
* @package Kohana
* @author Kohana Team
* @copyright (c) 2007-2012 Kohana Team
* @copyright (c) 2005 Harry Fuecks
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
*/
function _substr($str, $offset, $length = NULL)
{
if (UTF8::is_ascii($str)) {
return $length === NULL ? substr($str, $offset) : substr($str, $offset, $length);
}
// Normalize params
$str = (string) $str;
$strlen = UTF8::strlen($str);
$offset = (int) ($offset < 0) ? max(0, $strlen + $offset) : $offset;
// Normalize to positive offset
$length = $length === NULL ? NULL : (int) $length;
// Impossible
if ($length === 0 or $offset >= $strlen or $length < 0 and $length <= $offset - $strlen) {
return '';
}
// Whole string
if ($offset == 0 and ($length === NULL or $length >= $strlen)) {
return $str;
}
// Build regex
$regex = '^';
// Create an offset expression
if ($offset > 0) {
// PCRE repeating quantifiers must be less than 65536, so repeat when necessary
$x = (int) ($offset / 65535);
$y = (int) ($offset % 65535);
$regex .= $x == 0 ? '' : '(?:.{65535}){' . $x . '}';
$regex .= $y == 0 ? '' : '.{' . $y . '}';
}
// Create a length expression
if ($length === NULL) {
$regex .= '(.*)';
// No length set, grab it all
} elseif ($length > 0) {
// Reduce length so that it can't go beyond the end of the string
$length = min($strlen - $offset, $length);
$x = (int) ($length / 65535);
$y = (int) ($length % 65535);
$regex .= '(';
$regex .= $x == 0 ? '' : '(?:.{65535}){' . $x . '}';
$regex .= '.{' . $y . '})';
} else {
$x = (int) (-$length / 65535);
$y = (int) (-$length % 65535);
$regex .= '(.*)';
$regex .= $x == 0 ? '' : '(?:.{65535}){' . $x . '}';
$regex .= '.{' . $y . '}';
}
preg_match('/' . $regex . '/us', $str, $matches);
return $matches[1];
}
示例13: _strrpos
/**
* UTF8::strrpos
*
* @package Kohana
* @author Kohana Team
* @copyright (c) 2007-2012 Kohana Team
* @copyright (c) 2005 Harry Fuecks
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
*/
function _strrpos($str, $search, $offset = 0)
{
$offset = (int) $offset;
if (UTF8::is_ascii($str) and UTF8::is_ascii($search)) {
return strrpos($str, $search, $offset);
}
if ($offset == 0) {
$array = explode($search, $str, -1);
return isset($array[0]) ? UTF8::strlen(implode($search, $array)) : FALSE;
}
$str = UTF8::substr($str, $offset);
$pos = UTF8::strrpos($str, $search);
return $pos === FALSE ? FALSE : $pos + $offset;
}
示例14: _strpos
/**
* UTF8::strpos
*
* @package Kohana
* @author Kohana Team
* @copyright (c) 2007-2012 Kohana Team
* @copyright (c) 2005 Harry Fuecks
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
*/
function _strpos($str, $search, $offset = 0)
{
$offset = (int) $offset;
if (UTF8::is_ascii($str) and UTF8::is_ascii($search)) {
return strpos($str, $search, $offset);
}
if ($offset == 0) {
$array = explode($search, $str, 2);
return isset($array[1]) ? UTF8::strlen($array[0]) : false;
}
$str = UTF8::substr($str, $offset);
$pos = UTF8::strpos($str, $search);
return $pos === false ? false : $pos + $offset;
}
示例15: generate_challenge
/**
* Generates a new Captcha challenge.
*
* @return string The challenge answer
*/
public function generate_challenge()
{
// Load words from the current language and randomize them
$words = Config::get('captcha', 'words');
shuffle($words);
// Loop over each word...
foreach ($words as $word) {
// ...until we find one of the desired length
if (abs(Captcha::$config['complexity'] - UTF8::strlen($word)) < 2) {
return UTF8::strtoupper($word);
}
}
// Return any random word as final fallback
return UTF8::strtoupper($words[array_rand($words)]);
}