本文整理汇总了PHP中iconv_strlen函数的典型用法代码示例。如果您正苦于以下问题:PHP iconv_strlen函数的具体用法?PHP iconv_strlen怎么用?PHP iconv_strlen使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了iconv_strlen函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: jtpl_modifier_common_count_characters
/**
* Modifier plugin : count the number of characters in a text
*
* <pre>{$mytext|count_characters}
* {$mytext|count_characters:true}</pre>
* @param string $string
* @param boolean $include_spaces include whitespace in the character count
* @return integer
*/
function jtpl_modifier_common_count_characters($string, $include_spaces = false)
{
if ($include_spaces) {
return iconv_strlen($string, jTpl::getEncoding());
}
return preg_match_all("/[^\\s]/", $string, $match);
}
示例2: testIconvStrlen
/**
* @covers Symfony\Polyfill\Iconv\Iconv::iconv_strlen
* @covers Symfony\Polyfill\Iconv\Iconv::strlen1
* @covers Symfony\Polyfill\Iconv\Iconv::strlen2
*/
public function testIconvStrlen()
{
$this->assertSame(4, iconv_strlen('déjà', 'UTF-8'));
$this->assertSame(3, iconv_strlen('한국어', 'UTF-8'));
$this->assertSame(4, p::strlen2('déjà'));
$this->assertSame(3, p::strlen2('한국어'));
}
示例3: toASCII
/**
* Convert a string to ASCII
*
* @access public
* @param string $str The string to convert
* @param string $charset The default charset to use
* @return string The converted string
*/
public static function toASCII($str, $charset = 'UTF-8')
{
$asciistr = '';
if (mb_detect_encoding($str, 'UTF-8', true) === false) {
$str = utf8_encode($str);
}
if (version_compare(PHP_VERSION, '5.6.0') < 0) {
iconv_set_encoding('input_encoding', 'UTF-8');
iconv_set_encoding('internal_encoding', 'UTF-8');
iconv_set_encoding('output_encoding', $charset);
}
$str = html_entity_decode($str, ENT_QUOTES, $charset);
$strlen = iconv_strlen($str, $charset);
for ($i = 0; $i < $strlen; $i++) {
$char = iconv_substr($str, $i, 1, $charset);
if (!preg_match('/[`\'^~"]+/', $char)) {
if ('UTF-8' === $charset) {
$asciistr .= preg_replace('/[`\'^~"]+/', '', iconv($charset, 'ASCII//TRANSLIT//IGNORE', $char));
} else {
$asciistr .= preg_replace('/[`\'^~"]+/', '', iconv('UTF-8', $charset . '//TRANSLIT//IGNORE', $char));
}
} else {
$asciistr .= $char;
}
}
return $asciistr;
}
示例4: sanitize
/**
* Filter: removes unnecessary whitespace and shortens value to control's max length.
* @return string
*/
public function sanitize($value)
{
if ($this->control->maxlength && iconv_strlen($value, 'UTF-8') > $this->control->maxlength) {
$value = iconv_substr($value, 0, $this->control->maxlength, 'UTF-8');
}
return String::trim(strtr($value, "\r\n", ' '));
}
示例5: truncate
public function truncate($string, $length = 50, $chars = true, $postfix = '...')
{
$truncated = trim($string);
if (!$string) {
return $truncated;
}
$length = (int) $length;
if ($chars) {
$fullLength = iconv_strlen($truncated, 'UTF-8');
if ($fullLength > $length) {
$truncated = trim(iconv_substr($truncated, 0, $length, 'UTF-8')) . $postfix;
}
// words
} else {
$truncated = str_replace(' ', ' ', $truncated);
$words = explode(' ', $truncated);
$truncated = '';
$count = count($words);
for ($i = 0; $i < $count; $i++) {
$truncated .= $words[$i] . ' ';
if ($length == $i) {
$truncated .= $postfix;
break;
}
}
}
return $truncated;
}
示例6: login
public function login()
{
$email = strtolower($this->getParam('email'));
$password = $this->getParam('password');
$remember = $this->getParam('remember');
if (!$email || !$password) {
exit(json_encode(['result' => 'fail', 'message' => 'Все поля формы обязательны для заполнения']));
}
// if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
// exit(json_encode(['result' => 'fail', 'message' => 'Некорректный Email-адрес']));
// }
if (iconv_strlen($password) < Registry::get('min_password_length') || iconv_strlen($password) > Registry::get('max_password_length')) {
exit(json_encode(['result' => 'fail', 'message' => 'Длина пароля должна быть в пределах от ' . Registry::get('min_password_length') . ' до ' . Registry::get('max_password_length') . ' символов']));
}
$user = DB::run()->query("select * from users where email = '{$email}'")->fetch();
if (!$user) {
exit(json_encode(['result' => 'fail', 'message' => 'Данный Email-адрес в системе не зарегистрирован']));
}
$userPassHash = Tools::hash($password, Registry::get('hash_salt'));
if ($user->password != $userPassHash) {
exit(json_encode(['result' => 'fail', 'message' => 'Неверный пароль']));
}
if ($remember === 'true') {
Tools::setUserAuth($user, true);
} else {
Tools::setUserAuth($user);
}
exit(json_encode(['result' => 'done', 'message' => 'Успешный вход в систему!<br>Сейчас Вы будете перенаправлены.']));
}
示例7: cstr
public function cstr($text, $start = 0, $limit = 12)
{
if (function_exists('mb_substr')) {
$more = mb_strlen($text) > $limit ? TRUE : FALSE;
$text = mb_substr($text, 0, $limit, 'UTF-8');
return array($text, $more);
} elseif (function_exists('iconv_substr')) {
$more = iconv_strlen($text) > $limit ? TRUE : FALSE;
$text = iconv_substr($text, 0, $limit, 'UTF-8');
return array($text, $more);
} else {
preg_match_all("/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf]\r\n \t\t[x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]\r\n \t\t|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/", $text, $ar);
if (func_num_args() >= 3) {
if (count($ar[0]) > $limit) {
$more = TRUE;
$text = join("", array_slice($ar[0], 0, $limit));
}
$more = TRUE;
$text = join("", array_slice($ar[0], 0, $limit));
} else {
$more = FALSE;
$text = join("", array_slice($ar[0], 0));
}
return array($text, $more);
}
}
示例8: validate
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Length) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\Length');
}
if (null === $value || '' === $value) {
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
$stringValue = (string) $value;
if ('UTF8' === ($charset = strtoupper($constraint->charset))) {
$charset = 'UTF-8';
// iconv on Windows requires "UTF-8" instead of "UTF8"
}
$length = @iconv_strlen($stringValue, $charset);
$invalidCharset = false === $length;
if ($invalidCharset) {
$this->context->buildViolation($constraint->charsetMessage)->setParameter('{{ value }}', $this->formatValue($stringValue))->setParameter('{{ charset }}', $constraint->charset)->setInvalidValue($value)->setCode(Length::INVALID_CHARACTERS_ERROR)->addViolation();
return;
}
if (null !== $constraint->max && $length > $constraint->max) {
$this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)->setParameter('{{ value }}', $this->formatValue($stringValue))->setParameter('{{ limit }}', $constraint->max)->setInvalidValue($value)->setPlural((int) $constraint->max)->setCode(Length::TOO_LONG_ERROR)->addViolation();
return;
}
if (null !== $constraint->min && $length < $constraint->min) {
$this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->minMessage)->setParameter('{{ value }}', $this->formatValue($stringValue))->setParameter('{{ limit }}', $constraint->min)->setInvalidValue($value)->setPlural((int) $constraint->min)->setCode(Length::TOO_SHORT_ERROR)->addViolation();
}
}
示例9: msubstr
/**
* 字符串截取,支持中文和其他编码
* @static
* @access public
* @param string $str 需要转换的字符串
* @param string $start 开始位置
* @param string $length 截取长度
* @param string $charset 编码格式
* @param string $suffix 截断显示字符
* @return string
*/
function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = '...')
{
$total = 0;
if (function_exists("mb_substr")) {
$total = mb_strlen($str, $charset);
if ($total <= $length) {
return $str;
}
$slice = mb_substr($str, $start, $length, $charset);
} elseif (function_exists('iconv_substr')) {
$total = iconv_strlen($str, $charset);
if ($total <= $length) {
return $str;
}
$slice = iconv_substr($str, $start, $length, $charset);
if (false === $slice) {
$slice = '';
}
} else {
$re['utf-8'] = "/[-]|[�-�][�-�]|[�-�][�-�]{2}|[�-�][�-�]{3}/";
$re['gb2312'] = "/[-]|[�-�][�-�]/";
$re['gbk'] = "/[-]|[�-�][@-�]/";
$re['big5'] = "/[-]|[�-�]([@-~]|�-�])/";
preg_match_all($re[$charset], $str, $match);
$total = count($match[0]);
if ($total <= $length) {
return $str;
}
$slice = join("", array_slice($match[0], $start, $length));
}
return $suffix ? $slice . $suffix : $slice;
}
示例10: mb_strlen
function mb_strlen($str, $enc = FALSE)
{
if (function_exists('iconv_strlen')) {
return $enc ? iconv_strlen($str, $enc) : iconv_strlen($str);
}
return strlen($str);
}
示例11: strPad
/**
* String padding
*
* @param string $input
* @param integer $padLength
* @param string $padString
* @param integer $padType
* @param string $charset
* @return string
*/
public static function strPad($input, $padLength, $padString = ' ', $padType = STR_PAD_RIGHT, $charset = 'UTF-8')
{
$return = '';
$lengthOfPadding = $padLength - iconv_strlen($input, $charset);
$padStringLength = iconv_strlen($padString, $charset);
if ($padStringLength === 0 || $lengthOfPadding === 0) {
$return = $input;
} else {
$repeatCount = floor($lengthOfPadding / $padStringLength);
if ($padType === STR_PAD_BOTH) {
$lastStringLeft = '';
$lastStringRight = '';
$repeatCountLeft = $repeatCountRight = ($repeatCount - $repeatCount % 2) / 2;
$lastStringLength = $lengthOfPadding - 2 * $repeatCountLeft * $padStringLength;
$lastStringLeftLength = $lastStringRightLength = floor($lastStringLength / 2);
$lastStringRightLength += $lastStringLength % 2;
$lastStringLeft = iconv_substr($padString, 0, $lastStringLeftLength, $charset);
$lastStringRight = iconv_substr($padString, 0, $lastStringRightLength, $charset);
$return = str_repeat($padString, $repeatCountLeft) . $lastStringLeft . $input . str_repeat($padString, $repeatCountRight) . $lastStringRight;
} else {
$lastString = iconv_substr($padString, 0, $lengthOfPadding % $padStringLength, $charset);
if ($padType === STR_PAD_LEFT) {
$return = str_repeat($padString, $repeatCount) . $lastString . $input;
} else {
$return = $input . str_repeat($padString, $repeatCount) . $lastString;
}
}
}
return $return;
}
示例12: Len
public static function Len($str)
{
if (function_exists('mb_strlen')) {
$count = mb_strlen($str, __CFG::CHARSET);
} else {
if (function_exists('iconv_strlen')) {
$count = iconv_strlen($str, __CFG::CHARSET);
} else {
if (strtolower(__CFG::CHARSET) == 'utf-8') {
$i = 0;
$count = 0;
$len = strlen($str);
while ($i < $len) {
$chr = ord($str[$i]);
$count++;
$i++;
if ($i >= $len) {
break;
}
if ($chr & 0x80) {
$chr <<= 1;
while ($chr & 0x80) {
$i++;
$chr <<= 1;
}
}
}
} else {
$count = strlen($str);
}
}
}
return $count;
}
示例13: isValid
/**
* Defined by Zend_Validate_Interface
*
* Returns true if and only if the string length of $value is at least the min option and
* no greater than the max option (when the max option is not null).
*
* @param string $value
* @return boolean
*/
public function isValid($value)
{
if (!is_string($value)) {
$this->_error(self::INVALID);
return false;
}
$this->_setValue($value);
if ($this->_encoding !== null) {
$length = iconv_strlen($value, $this->_encoding);
} else {
$length = iconv_strlen($value, 'UTF-8');
}
$counter = array_count_values(App_Util_String::strToArray($value));
foreach (static::$doubleCountChars as $char) {
if (isset($counter[$char])) {
$length += $counter[$char];
}
}
if ($length < $this->_min) {
$this->_error(self::TOO_SHORT);
}
if (null !== $this->_max && $this->_max < $length) {
$this->_error(self::TOO_LONG);
}
if (count($this->_messages)) {
return false;
} else {
return true;
}
}
示例14: execute
/**
* Reset forgotten password
*
* Used to handle data received from reset forgotten password form
*
* @return void
*/
public function execute()
{
$resetPasswordToken = (string) $this->getRequest()->getQuery('token');
$customerId = (int) $this->getRequest()->getQuery('id');
$password = (string) $this->getRequest()->getPost('password');
$passwordConfirmation = (string) $this->getRequest()->getPost('confirmation');
if ($password !== $passwordConfirmation) {
$this->messageManager->addError(__("New Password and Confirm New Password values didn't match."));
return;
}
if (iconv_strlen($password) <= 0) {
$this->messageManager->addError(__('New password field cannot be empty.'));
$this->_redirect('*/*/createPassword', array('id' => $customerId, 'token' => $resetPasswordToken));
return;
}
try {
$this->_customerAccountService->resetPassword($customerId, $resetPasswordToken, $password);
$this->messageManager->addSuccess(__('Your password has been updated.'));
$this->_redirect('*/*/login');
return;
} catch (\Exception $exception) {
$this->messageManager->addError(__('There was an error saving the new password.'));
$this->_redirect('*/*/createPassword', array('id' => $customerId, 'token' => $resetPasswordToken));
return;
}
}
示例15: truncate
/**
* Truncates the $string passed in, from the $start to the $length, and optionally
* appends a $prefix and/or $postfix, to the string to indicate it is truncated.
*
* @param string $string
* The string to truncate.
*
* @param int $start
* The starting index. 0 by default.
*
* @param int $length
* The length after which the string should be truncated. Default 50.
*
* @param string $prefix
* The prefix to be prepended to the $string. Default is '...'.
*
* @param string $postfix
* The postfix to be appended to the $string. Default is '...'.
*
* @return string
* The truncated string.
*/
public function truncate($string, $start = 0, $length = 50, $prefix = '...', $postfix = '...')
{
$truncated = trim($string);
$start = (int) $start;
$length = (int) $length;
// Return original string if max length is 0
if ($length < 1) {
return $truncated;
}
$full_length = iconv_strlen($truncated);
// Truncate if necessary
if ($full_length > $length) {
// Right-clipped
if ($length + $start > $full_length) {
$start = $full_length - $length;
$postfix = '';
}
// Left-clipped
if ($start == 0) {
$prefix = '';
}
// Do truncate!
$truncated = $prefix . trim(substr($truncated, $start, $length)) . $postfix;
}
return $truncated;
}