本文整理汇总了PHP中utf8::substr方法的典型用法代码示例。如果您正苦于以下问题:PHP utf8::substr方法的具体用法?PHP utf8::substr怎么用?PHP utf8::substr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类utf8
的用法示例。
在下文中一共展示了utf8::substr方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _str_pad
/**
* utf8::str_pad
*
* @package Core
* @author Kohana Team
* @copyright (c) 2007 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_left);
return $pad_left . $str . $pad_right;
}
trigger_error('utf8::str_pad: Unknown padding type (' . $type . ')', E_USER_ERROR);
}
示例2: __construct
public function __construct()
{
parent::__construct();
if (!config::item('news_blog', 'news') && uri::segment(1) != 'news') {
router::redirect('news/' . utf8::substr(uri::getURI(), 5));
}
}
示例3: days
/**
* Returns an array of the names of the days, using the current locale.
*
* @param integer left of day names
* @return array
*/
public static function days($length = TRUE)
{
// strftime day format
$format = $length > 3 ? '%A' : '%a';
// Days of the week
$days = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
if (Calendar::$start_monday === TRUE) {
// Push Sunday to the end of the days
array_push($days, array_shift($days));
}
if (strpos(Kohana::config('locale.language.0'), 'en') !== 0) {
// This is a bit awkward, but it works properly and is reliable
foreach ($days as $i => $day) {
// Convert the English names to i18n names
$days[$i] = strftime($format, strtotime($day));
}
}
if (is_int($length) or ctype_digit($length)) {
foreach ($days as $i => $day) {
// Shorten the days to the expected length
$days[$i] = utf8::substr($day, 0, $length);
}
}
return $days;
}
示例4: __construct
public function __construct()
{
parent::__construct();
if (!config::item('news_active', 'news')) {
error::show404();
} elseif (!session::permission('news_access', 'news')) {
view::noAccess();
} elseif (config::item('news_blog', 'news') && uri::segment(1) != 'blog') {
router::redirect('blog/' . utf8::substr(uri::getURI(), 5));
}
loader::model('news/news');
}
示例5: number_to_text
public static function number_to_text($value, $separator = ' ')
{
$digits = (string) $value;
if (utf8::strpos($digits, '.') !== FALSE) {
$digits = explode('.', $digits);
return static::number_to_text($digits[0]) . $separator . static::number_to_text($digits[1]);
}
$jednosci = array('zero', 'jeden', 'dwa', 'trzy', 'cztery', 'pięć', 'sześć', 'siedem', 'osiem', 'dziewięć');
$dziesiatki = array('', 'dziesięć', 'dwadzieścia', 'trzydzieści', 'czterdzieści', 'piećdziesiąt', 'sześćdziesiąt', 'siedemdziesiąt', 'osiemdziesiąt', 'dziewiećdziesiąt');
$setki = array('', 'sto', 'dwieście', 'trzysta', 'czterysta', 'piećset', 'sześćset', 'siedemset', 'osiemset', 'dziewiećset');
$nastki = array('dziesieć', 'jedenaście', 'dwanaście', 'trzynaście', 'czternaście', 'piętnaście', 'szesnaście', 'siedemnaście', 'osiemnaście', 'dzięwietnaście');
$tysiace = array('tysiąc', 'tysiące', 'tysięcy');
$digits = (string) $value;
$digits = utf8::strrev($digits);
$i = utf8::strlen($digits);
$string = '';
if ($i > 5 && $digits[5] > 0) {
$string .= $setki[$digits[5]] . ' ';
}
if ($i > 4 && $digits[4] > 1) {
$string .= $dziesiatki[$digits[4]] . ' ';
} elseif ($i > 3 && $digits[4] == 1) {
$string .= $nastki[$digits[3]] . ' ';
}
if ($i > 3 && $digits[3] > 0 && $digits[4] != 1) {
$string .= $jednosci[$digits[3]] . ' ';
}
$tmpStr = utf8::substr(utf8::strrev($digits), 0, -3);
if (utf8::strlen($tmpStr) > 0) {
$tmpInt = (int) $tmpStr;
if ($tmpInt == 1) {
$string .= $tysiace[0] . ' ';
} elseif ($tmpInt % 10 > 1 && $tmpInt % 10 < 5 && ($tmpInt < 10 || $tmpInt > 20)) {
$string .= $tysiace[1] . ' ';
} else {
$string .= $tysiace[2] . ' ';
}
}
if ($i > 2 && $digits[2] > 0) {
$string .= $setki[$digits[2]] . ' ';
}
if ($i > 1 && $digits[1] > 1) {
$string .= $dziesiatki[$digits[1]] . ' ';
} elseif ($i > 0 && $digits[1] == 1) {
$string .= $nastki[$digits[0]] . ' ';
}
if ($digits[0] > 0 && $digits[1] != 1) {
$string .= $jednosci[$digits[0]] . ' ';
}
return $string;
}
示例6: _strrpos
/**
* utf8::strrpos
*
* @package Kohana
* @author Kohana Team
* @copyright (c) 2007-2008 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;
}
示例7: limit_chars
/**
* Limits a phrase to a given number of characters.
*
* @param string phrase to limit characters of
* @param integer number of characters to limit to
* @param string end character or entity
* @param boolean enable or disable the preservation of words while limiting
* @return string
*/
public static function limit_chars($str, $limit = 100, $end_char = NULL, $preserve_words = FALSE)
{
$end_char = $end_char === NULL ? '…' : $end_char;
$limit = (int) $limit;
if (trim($str) === '' or utf8::strlen($str) <= $limit) {
return $str;
}
if ($limit <= 0) {
return $end_char;
}
if ($preserve_words == FALSE) {
return rtrim(utf8::substr($str, 0, $limit)) . $end_char;
}
preg_match('/^.{' . ($limit - 1) . '}\\S*/us', $str, $matches);
return rtrim($matches[0]) . (strlen($matches[0]) == strlen($str) ? '' : $end_char);
}
示例8: _strcspn
/**
* utf8::strcspn
*
* @package Core
* @author Kohana Team
* @copyright (c) 2007 Kohana Team
* @copyright (c) 2005 Harry Fuecks
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
*/
function _strcspn($str, $mask, $offset = NULL, $length = NULL)
{
if ($str == '' or $mask == '') {
return 0;
}
if (utf8::is_ascii($str) and utf8::is_ascii($mask)) {
return $offset === NULL ? strcspn($str, $mask) : ($length === NULL ? strcspn($str, $mask, $offset) : strcspn($str, $mask, $offset, $length));
}
if ($start !== NULL or $length !== NULL) {
$str = utf8::substr($str, $offset, $length);
}
// Escape these characters: - [ ] . : \ ^ /
// The . and : are escaped to prevent possible warnings about POSIX regex elements
$mask = preg_replace('#[-[\\].:\\\\^/]#', '\\\\$0', $mask);
preg_match('/^[^' . $mask . ']+/u', $str, $matches);
return isset($matches[0]) ? utf8::strlen($matches[0]) : 0;
}
示例9: limit_chars
/**
* Limits a phrase to a given number of characters.
*
* $text = static::limit_chars($text);
*
* @param string phrase to limit characters of
* @param integer number of characters to limit to
* @param string end character or entity
* @param boolean enable or disable the preservation of words while limiting
* @return string
* @uses \utf8::strlen
*/
public static function limit_chars($str, $limit = 100, $end_char = null, $preserve_words = false)
{
$end_char = $end_char === null ? '…' : $end_char;
$limit = (int) $limit;
if (\trim($str) === '' || \utf8::strlen($str) <= $limit) {
return $str;
}
if ($limit <= 0) {
return $end_char;
}
if ($preserve_words === false) {
return \rtrim(\utf8::substr($str, 0, $limit)) . $end_char;
}
// Don't preserve words. The limit is considered the top limit.
// No strings with a length longer than $limit should be returned.
if (!\preg_match('/^.{0,' . $limit . '}\\s/us', $str, $matches)) {
return $end_char;
}
return \rtrim($matches[0]) . (\strlen($matches[0]) === \strlen($str) ? '' : $end_char);
}
示例10: detail
/**
* 获取文章详情
* @param int $id
* @return array $data;
*/
public function detail($id)
{
// $memkey = MEMPREFIX . 'article:detail' . $id;
$data = false;
if (is_numeric($id)) {
//$data = $this->memcache->get($memkey);
// $data = false;
if ($data == false) {
$detail = $this->db->get('news', ['id', 'catid', 'title', 'keywords', 'description', 'listorder', 'uid', 'username', 'url', 'islink', 'inputtime', 'updatetime'], ['AND' => ['id' => $id, 'status' => 99]]);
// $data != false;
if ($detail != false) {
$data = $this->db->get('news_data', ['content', 'fromweb'], ['id' => $id]);
$data == false && ($data = []);
$data = array_merge($detail, $data);
$data['description'] == false && ($data['description'] = utf8::substr(strip_tags($data['content']), 0, 78) . '...');
}
// $data <> false && $this->memcache->set($memkey, $data, MEMCACHE_COMPRESSED, 3600 * 24);
}
}
return $data;
}
示例11: days
/**
* Returns an array of the names of the days, using the current locale.
*
* @param integer left of day names
* @return array
*/
public static function days($length = TRUE)
{
// strftime day format
$format = $length > 3 ? '%A' : '%a';
// Days of the week
for ($i = 0; $i < 7; $i++) {
// $days[] = strftime($format, strtotime('+'.$i.' day', 300000));
// $days[] = strftime($format, ($i * 60*60*24 + 300000));
$days[] = strftime($format, $i * 86400 + 300000);
}
if (Calendar::$start_monday === TRUE) {
// Push Sunday to the end of the days
array_push($days, array_shift($days));
}
if (is_int($length) or ctype_digit($length)) {
foreach ($days as $i => $day) {
// Shorten the days to the expected length
$days[$i] = utf8::substr($day, 0, $length);
}
}
return $days;
}
示例12: upload
public function upload()
{
if (!$_FILES) {
return;
}
$surfix = Kohana::config('torn')->surfix->temp;
$surfix_len = utf8::strlen($surfix);
foreach ($_POST as $key => $tmp_name) {
if (utf8::substr($key, -$surfix_len) == $surfix) {
$field = utf8::substr($key, 0, -$surfix_len);
$this->parent->model->set($field, $tmp_name);
}
}
$cache = Cache::instance();
foreach ($_FILES as $key => $upload) {
$this->parent->model->set($key, $upload);
if (!isset($this->parent->fields[$key]) or !$this->parent->fields[$key] instanceof Torn_Field_File) {
continue;
}
if (upload::not_empty($upload) and upload::valid($upload)) {
$seed = Arr::get($_POST, '__SEED__', md5(Request::current()->uri() . time()));
$tmp_name = $seed . '-' . md5_file($upload['tmp_name']);
if (upload::save($upload, $tmp_name, Kohana::$cache_dir) !== FALSE) {
$timestamp = 24 * 60 * 60;
$cache->set($tmp_name, array('upload' => $upload, 'timestamp' => $timestamp), $timestamp);
$tmp_old_file = Arr::get($_POST, $key . $surfix);
if (!empty($tmp_old_file) and file_exists(Kohana::$cache_dir . DIRECTORY_SEPARATOR . $tmp_old_file)) {
try {
unlink(Kohana::$cache_dir . DIRECTORY_SEPARATOR . $tmp_old_file);
$cache->delete($tmp_old_file);
} catch (Exception $e) {
}
}
$this->parent->model->set($key, $tmp_name);
}
}
}
}
示例13: account_number
public function account_number($p_iNRB)
{
// Usuniecie spacji
$iNRB = utf8::str_ireplace(' ', '', $p_iNRB);
// Sprawdzenie czy przekazany numer zawiera 26 znaków
if (utf8::strlen($iNRB) != 26) {
die('fail #1');
return false;
}
// Zdefiniowanie tablicy z wagami poszczególnych cyfr
$aWagiCyfr = array(1, 10, 3, 30, 9, 90, 27, 76, 81, 34, 49, 5, 50, 15, 53, 45, 62, 38, 89, 17, 73, 51, 25, 56, 75, 71, 31, 19, 93, 57);
// Dodanie kodu kraju (w tym przypadku dodajemy kod PL)
$iNRB = $iNRB . '2521';
$iNRB = utf8::substr($iNRB, 2) . utf8::substr($iNRB, 0, 2);
// Wyzerowanie zmiennej
$iSumaCyfr = 0;
// Pćtla obliczająca sumć cyfr w numerze konta
for ($i = 0; $i < 30; $i++) {
$iSumaCyfr += $iNRB[29 - $i] * $aWagiCyfr[$i];
}
// Sprawdzenie czy modulo z sumy wag poszczegolnych cyfr jest rowne 1
return $iSumaCyfr % 97 == 1;
}
示例14: render
/**
* Outputs the Captcha image.
*
* @param boolean $html HTML output
* @return mixed
*/
public function render($html = TRUE)
{
// Creates $this->image
$this->image_create(Captcha::$config['background']);
// Add a random gradient
if (empty(Captcha::$config['background'])) {
$color1 = imagecolorallocate($this->image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(150, 255));
$color2 = imagecolorallocate($this->image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(150, 255));
$this->image_gradient($color1, $color2);
}
// Add a few random lines
for ($i = 0, $count = mt_rand(5, Captcha::$config['complexity'] * 4); $i < $count; $i++) {
$color = imagecolorallocatealpha($this->image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(100, 255), mt_rand(50, 120));
imageline($this->image, mt_rand(0, Captcha::$config['width']), 0, mt_rand(0, Captcha::$config['width']), Captcha::$config['height'], $color);
}
// Calculate character font-size and spacing
$default_size = min(Captcha::$config['width'], Captcha::$config['height'] * 2) / (utf8::strlen($this->response) + 1);
$spacing = (int) (Captcha::$config['width'] * 0.9 / utf8::strlen($this->response));
// Draw each Captcha character with varying attributes
for ($i = 0, $strlen = utf8::strlen($this->response); $i < $strlen; $i++) {
// Use different fonts if available
$font = Captcha::$config['fontpath'] . Captcha::$config['fonts'][array_rand(Captcha::$config['fonts'])];
// Allocate random color, size and rotation attributes to text
$color = imagecolorallocate($this->image, mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150));
$angle = mt_rand(-40, 20);
// Scale the character size on image height
$size = $default_size / 10 * mt_rand(8, 12);
$box = imageftbbox($size, $angle, $font, utf8::substr($this->response, $i, 1));
// Calculate character starting coordinates
$x = $spacing / 4 + $i * $spacing;
$y = Captcha::$config['height'] / 2 + ($box[2] - $box[5]) / 4;
// Write text character to image
imagefttext($this->image, $size, $angle, $x, $y, $color, $font, utf8::substr($this->response, $i, 1));
}
// Output
return $this->image_render($html);
}
示例15: shorten_string
public static function shorten_string($str, $max_length, $mid_cut = false)
{
if (!is_scalar($str)) {
return false;
}
if (!is_int($max_length)) {
return false;
}
$length = utf8::strlen($str);
if ($length <= $max_length) {
return $str;
} elseif ($mid_cut) {
$mid = (int) ceil($max_length / 2);
$string = utf8::substr($str, 0, $mid) . '...' . utf8::substr($str, $mid);
} else {
return utf8::substr($str, 0, $max_length) . '...';
}
}