/** * 截取字符串 * @param $st * 要截取的字符串 * @param $start * 开始位置 * @param $length * 截取的长度 * @param $charset * 字符编码 * @param $suffix * 是否显示 "... " * @return String * */ public static function msubstr($str, $start = 0, $length, $charset = 'UTF8', $suffix = true) { //字符占位宽 $width = 0; //处理后的字符串 $cut_str = ''; //debug if ($charset == 'utf-8') { $charset = 'UTF8'; } if (function_exists('mb_strlen')) { $width = (strlen($str) + mb_strlen($str, $charset)) / 2; } else { $width = strlen($str) / 2; } if ($width > $length) { if (function_exists('mb_substr')) { $cut_str = mb_substr($str, $start, $length, $charset); } elseif (function_exists('iconv_substr')) { $cut_str = iconv_substr($str, $start, $length, $charset); } } else { $cut_str = $str; } if ($suffix) { $cut_str = $cut_str . '...'; } return $cut_str; }
public function posts_list() { $this->display('Public:head'); $tag = I('get.tag'); $c_tag = $this->tag_exists($tag); $db_name = CONTROLLER_NAME . '_' . $tag; $m = M($db_name); $count = $m->where(array('isT' => 1))->count(); //分页 $Page = new \Think\Page($count, 10); $Page->setConfig('theme', '当前第%NOW_PAGE%页 共%TOTAL_PAGE%页 %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% <b>%HEADER%</b> '); $pager = $Page->show(); $posts = $m->where(array('isT' => 1))->order('last_post_time desc')->limit($Page->firstRow . ',' . $Page->listRows)->select(); foreach ($posts as $k => $v) { $posts[$k]['content'] = iconv_substr($v['content'], 0, 300, 'utf-8'); } //dump($posts); //dump($pager); // exit; $this->assign('tag', $tag); $this->assign('c_tag', $c_tag); $this->assign('posts', $posts); $this->assign('pager', $pager); $this->display(); $this->display('Public:foot'); }
/** * Saving visitor information * * @param Mage_Log_Model_Visitor $visitor * @return Mage_Log_Model_Mysql4_Visitor */ protected function _saveVisitorInfo($visitor) { $write = $this->_getWriteAdapter(); $data = array('visitor_id' => $visitor->getId(), 'http_referer' => iconv_substr($visitor->getHttpReferer(), 0, 250), 'http_user_agent' => $visitor->getHttpUserAgent(), 'http_accept_charset' => $visitor->getHttpAcceptCharset(), 'http_accept_language' => $visitor->getHttpAcceptLanguage(), 'server_addr' => $visitor->getServerAddr(), 'remote_addr' => $visitor->getRemoteAddr()); $write->insert($this->getTable('log/visitor_info'), $data); return $this; }
function adjDBValue($value, $len = -1) { if ($value === null) { return null; } if (is_array($value)) { foreach ($value as $key => $val) { $value[$key] = adjDBValue($val, $len); } } else { $value = trim($value); // NON-Multibyte here! if (strlen($value) == 0) { // NON-Multibyte here! return null; } if ($len >= 0) { // Multibyte here! if (defined('R3_APP_CHARSET_DB')) { // PHP-BUG (iconv_substr($value, 0, 1, "UFT-8"); faild!) if (iconv_strlen($value, R3_APP_CHARSET_DB) > 1) { $value = iconv_substr($value, 0, $len, R3_APP_CHARSET_DB); } } else { $value = substr($value, 0, $len); } } } return $value; }
/** * 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", ' ')); }
function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = true) { if (function_exists("mb_substr")) { if ($suffix) { return mb_substr($str, $start, $length, $charset) . "..."; } else { return mb_substr($str, $start, $length, $charset); } } elseif (function_exists('iconv_substr')) { if ($suffix) { return iconv_substr($str, $start, $length, $charset) . "..."; } else { return iconv_substr($str, $start, $length, $charset); } } $re['utf-8'] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef]\r\n [x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/"; $re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/"; $re['gbk'] = "/[x01-x7f]|[x81-xfe][x40-xfe]/"; $re['big5'] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/"; preg_match_all($re[$charset], $str, $match); $slice = join("", array_slice($match[0], $start, $length)); if ($suffix) { return $slice . "…"; } return $slice; }
/** * Filter: removes unnecessary whitespace and shortens value to control's max length. * @return string */ public function sanitize($value) { if ($this->control->maxlength && Nette\Utils\Strings::length($value) > $this->control->maxlength) { $value = iconv_substr($value, 0, $this->control->maxlength, 'UTF-8'); } return Nette\Utils\Strings::trim(strtr($value, "\r\n", ' ')); }
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; }
public function updateAction() { //处理接收参数 $id = intval($this->_getParam('id')); $pid = intval($this->_getParam('pid')); $catName = iconv_substr($this->_getParam('catname'), 0, 100, 'utf-8'); $description = iconv_substr($this->_getParam('description'), 0, 500, 'utf-8'); $display = intval($this->_getParam('display')); $order = intval($this->_getParam('order')); //是否显示赋值 $display !== 0 && ($display = 1); if (empty($catName)) { Zshop_Message::show($this, '带*号内容必须输入', 'back', 2); } else { $catalog = array('pid' => $pid, 'catname' => $catName, 'display' => $display, 'description' => $description, 'order' => $order); $isUpdated = $this->goodsCatalogModel->update($catalog, $id); if ($isUpdated) { $message = '修改成功'; $url = 'admin/goodscatalog/index'; } else { $message = '修改失败'; $url = 'back'; } Zshop_Message::show($this, $message, $url, 2); } }
/** * 截取UTF-8下字符串的函数 * * @param string $str * @param int $length * @param bool $append * * @return string */ protected static function sub_str($str, $length = 0, $append = true) { $str = trim($str); $strlength = strlen($str); if ($length == 0 || $length >= $strlength) { return false; } else { if ($length < 0) { $length = $strlength + $length; if ($length < 0) { $length = $strlength; } } } if (function_exists('mb_substr')) { $newstr = mb_substr($str, 0, $length, 'UTF-8'); } else { if (function_exists('iconv_substr')) { $newstr = iconv_substr($str, 0, $length, 'UTF-8'); } else { $newstr = substr($str, 0, $length); } } if ($append && $str != $newstr) { $newstr .= '...'; } return $newstr; }
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); } }
/** * cut string * * @param string $value * @param int $len * @param string $encode * @return string */ public static function Cut($value, $len, $encode = "UTF-8") { if (empty($value)) { return $value; } return iconv_substr($value, 0, $len, $encode); }
function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = true) { if (function_exists("mb_substr")) { if ($suffix && strlen($str) > $length) { return mb_substr($str, $start, $length, $charset) . "..."; } else { return mb_substr($str, $start, $length, $charset); } } elseif (function_exists('iconv_substr')) { if ($suffix && strlen($str) > $length) { return iconv_substr($str, $start, $length, $charset) . "..."; } else { return iconv_substr($str, $start, $length, $charset); } } $re['utf-8'] = "/[-]|[�-�][�-�]|[�-�][�-�]{2}|[�-�][�-�]{3}/"; $re['gb2312'] = "/[-]|[�-�][�-�]/"; $re['gbk'] = "/[-]|[�-�][@-�]/"; $re['big5'] = "/[-]|[�-�]([@-~]|�-�])/"; preg_match_all($re[$charset], $str, $match); $slice = join("", array_slice($match[0], $start, $length)); if ($suffix) { return $slice . "…"; } return $slice; }
/** * 字符串截取,支持中文和其他编码 * @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; }
/** * 字符串截取,支持中文和其他编码 * @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 = false) { if (function_exists("mb_strimwidth")) { $slice = mb_strimwidth($str, $start, $length, '', $charset); // mb_strimwidth 截取字符串 中文算2个字节,英文算1个 } elseif (function_exists('mb_substr')) { $slice = mb_substr($str, $start, $length, $charset); } elseif (function_exists("iconv_substr")) { $slice = iconv_substr($str, $start, $length, $charset); } else { $re['utf-8'] = "/[-]|[�-�][�-�]|[�-�][�-�]{2}|[�-�][�-�]{3}/"; $re['gb2312'] = "/[-]|[�-�][�-�]/"; $re['gbk'] = "/[-]|[�-�][@-�]/"; $re['big5'] = "/[-]|[�-�]([@-~]|�-�])/"; preg_match_all($re[$charset], $str, $match); $slice = join("", array_slice($match[0], $start, $length)); } if ($suffix) { return mb_strwidth($slice, $charset) > $length ? $slice : $slice . '...'; // mb_strwidth 计算字符串长度 中文算2个字节,英文算1个 } else { return $slice; } // return $suffix ? $slice.'...' : $slice; }
/** * Filter: shortens value to control's max length. * @return string */ protected function checkMaxLength($value) { if ($this->control->maxlength && iconv_strlen($value, 'UTF-8') > $this->control->maxlength) { $value = iconv_substr($value, 0, $this->control->maxlength, 'UTF-8'); } return $value; }
function sub_str($str, $length = 0, $append = true) { $str = strip_tags(trim($str)); $strlength = strlen($str); define('EC_CHARSET', 'utf-8'); if ($length == 0 || $length >= $strlength) { return $str; //截取长度等于0或大于等于本字符串的长度,返回字符串本身 } elseif ($length < 0) { $length = $strlength + $length; //那么截取长度就等于字符串长度减去截取长度 if ($length < 0) { $length = $strlength; //如果截取长度的绝对值大于字符串本身长度,则截取长度取字符串本身的长度 } } if (function_exists('mb_substr')) { $newstr = mb_substr($str, 0, $length, EC_CHARSET); } elseif (function_exists('iconv_substr')) { $newstr = iconv_substr($str, 0, $length, EC_CHARSET); } else { //$newstr = trim_right(substr($str, 0, $length)); $newstr = substr($str, 0, $length); } if ($append && $str != $newstr) { $newstr .= '...'; } return $newstr; }
/** * 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; }
public function getShortText() { if (strlen($this->text) > self::TEXT_LENGTH) { return iconv_substr(strip_tags($this->text), 0, self::TEXT_LENGTH, 'utf-8') . '...'; } return strip_tags($this->text); }
/** * 截取UTF-8编码下字符串的函数 * * @param string $str 被截取的字符串 * @param int $length 截取的长度 * @param bool $append 是否附加省略号 * * @return string */ function sub_str($str, $length = 0, $append = true) { $str = trim($str); $strlength = strlen($str); if ($length == 0 || $length >= $strlength) { return $str; } elseif ($length < 0) { $length = $strlength + $length; if ($length < 0) { $length = $strlength; } } if (function_exists('mb_substr')) { $newstr = mb_substr($str, 0, $length, EC_CHARSET); } elseif (function_exists('iconv_substr')) { $newstr = iconv_substr($str, 0, $length, EC_CHARSET); } else { //$newstr = trim_right(substr($str, 0, $length)); $newstr = substr($str, 0, $length); } if ($append && $str != $newstr) { $newstr .= '...'; } return $newstr; }
/** * Filter: shortens value to control's max length. * @return string */ public function checkMaxLength($value) { if ($this->control->maxlength && Nette\String::length($value) > $this->control->maxlength) { $value = iconv_substr($value, 0, $this->control->maxlength, 'UTF-8'); } return $value; }
/** * 获取文章内容 * * @param int $id 文章Id * @param bool $with_content 是否返回正文 * @return mixed */ public function getArticle($id, $with_content = true) { $id = sprintf('%u', $id); $item = $this->db->query("SELECT * FROM `supe_article_source` WHERE source_id = :id", array('id' => $id)); if ($item) { $item = $item[0]; $item['keywords'] = trim($item['keywords']); if ($item['keywords']) { $item['keywords'] = preg_split('/[ ,]+/', $item['keywords']); } else { $item['keywords'] = array(); } $item['picture'] = $this->getUploaderUrl($item['picture'], 200); if ($with_content) { $item['content'] = (string) $this->getArticleContent($id); if (empty($item['abstract'])) { $item['abstract'] = trim(strip_tags($item['content'])); $item['abstract'] = iconv_substr($item['abstract'], 0, 100, 'utf-8'); } } return $item; } else { return false; } }
public function render($key = '') { // 图片宽(px) $this->imageW || ($this->imageW = $this->codeLen * $this->fontSize + $this->codeLen * $this->fontSize / 2); // 图片高(px) $this->imageH || ($this->imageH = $this->fontSize * 1.5); // 建立一幅 $this->imageW x $this->imageH 的图像 $this->_image = imagecreate($this->imageW, $this->imageH); // 设置背景 imagecolorallocate($this->_image, $this->bg[0], $this->bg[1], $this->bg[2]); // 验证码字体随机颜色 $this->_color = imagecolorallocate($this->_image, mt_rand(1, 150), mt_rand(1, 150), mt_rand(1, 150)); // 验证码使用随机字体 $ttfPath = dirname(__FILE__) . '/captcha/' . ($this->useZh ? 'zhttfs' : 'ttfs') . '/'; $this->fontttf = $ttfPath . $this->fontttf; if ($this->useNoise) { // 绘杂点 $this->_writeNoise(); } if ($this->useCurve) { // 绘干扰线 $this->_writeCurve(); } // 绘验证码 $code = array(); // 验证码 $codeNX = -20; // 验证码第N个字符的左边距 if ($this->useZh) { // 中文验证码 for ($i = 0; $i < $this->codeLen; $i++) { $code[$i] = iconv_substr($this->zhSet, floor(mt_rand(0, mb_strlen($this->zhSet, 'utf-8') - 1)), 1, 'utf-8'); imagettftext($this->_image, $this->fontSize, mt_rand(-40, 40), $this->fontSize * ($i + 1) * 1.5, $this->fontSize + mt_rand(10, 20), $this->_color, $this->fontttf, $code[$i]); } } else { for ($i = 0; $i < $this->codeLen; $i++) { $code[$i] = $this->codeSet[mt_rand(0, strlen($this->codeSet) - 1)]; $codeNX += mt_rand($this->fontSize * 1.2, $this->fontSize * 1.4); //echo $codeNX.'<br/>'; imagettftext($this->_image, $this->fontSize, mt_rand(-30, 30), $codeNX, $this->fontSize * 1.3, $this->_color, $this->fontttf, $code[$i]); } } // 保存验证码 $key = $this->authcode($this->seKey) . $key; $code = $this->authcode(strtoupper(implode('', $code))); $secode = array(); $secode['verify_code'] = $code; // 把校验码保存到session $secode['verify_time'] = $this->_nowTime->timeStamp; // 验证码创建时间 Util_Session::set($key, $secode); header('Cache-Control: private, max-age=0, no-store, no-cache, must-revalidate'); header('Cache-Control: post-check=0, pre-check=0', false); header('Pragma: no-cache'); header("content-type: image/png"); // 输出图像 imagepng($this->_image); imagedestroy($this->_image); }
public function substr($start, $length = null) { if (null === $length) { return new self(iconv_substr($this->buffer, $start)); } else { return new self(iconv_substr($this->buffer, $start, $length)); } }
function utf8_substr($string, $offset, $length = null) { if ($length == null) { return iconv_substr($string, $offset, utf8_strlen($string), 'UTF-8'); } else { return iconv_substr($string, $offset, $length, 'UTF-8'); } }
public static function subText($text, $legth = 200, $textEnd = '...') { if (strlen($text) > $legth) { return iconv_substr($text, 0, $legth, "UTF-8") . $textEnd; } else { return $text; } }
/** * Split word into hypens * * Takes a word as a string and should return an array containing arrays of * two words, which each represent a possible split of a word. The german * word "Zuckerstück" for example changes its hyphens depending on the * splitting point, so the return value would look like: * * <code> * array( * array( 'Zuk-', 'kerstück' ), * array( 'Zucker-', 'stück' ), * ) * </code> * * You should always also include the concatenation character in the split * words, since it might change depending on the used language. * * @param mixed $word * @return void */ public function splitWord($word) { $splits = array(); for ($i = 1; $i < iconv_strlen($word, 'UTF-8'); ++$i) { $splits[] = array(iconv_substr($word, 0, $i) . '-', iconv_substr($word, $i)); } return $splits; }
protected function makeCode($codeLength = self::ROBOT_CHALLENGE_VERIFY_LENGTH) { $str = $this->codeText(); $code = ''; for ($i = 0; $i < $codeLength; $i++) { $code .= iconv_substr($str, mt_rand(1, mb_strlen($str)) - 1, 1); } return $code; }
/** * 字符截取(对中文、英文都可以进行截取) * @param string $string 字符串 * @param int $start 字符串截取开始位置 * @param int $length 字符串截取长度(多少个中文、英文) * @param string $charset 字符串编码 * @param string $dot 截取操作发生时,在被截取字符串最后边增加的字符串 * @param author chen teacher * @return string */ function str_cut(&$string, $start, $length, $charset = "utf-8", $dot = '...') { if (function_exists('mb_substr')) { if (mb_strlen($string, $charset) > $length) { //按字符获取长度 return mb_substr($string, $start, $length, $charset) . $dot; } return mb_substr($string, $start, $length, $charset); //按字符截取字符串 } else { if (function_exists('iconv_substr')) { if (iconv_strlen($string, $charset) > $length) { // return iconv_substr($string, $start, $length, $charset) . $dot; } return iconv_substr($string, $start, $length, $charset); } } $charset = strtolower($charset); switch ($charset) { case "utf-8": preg_match_all("/[-]|[�-�][�-�]|�[�-�][�-�]|[�-�][�-�][�-�]|�[�-�][�-�][�-�]|[�-�][�-�][�-�][�-�]/", $string, $ar); if (func_num_args() >= 3) { //func_num_args() 返回函数的参数个数 if (count($ar[0]) > $length) { return join("", array_slice($ar[0], $start, $length)) . $dot; } return join("", array_slice($ar[0], $start, $length)); } else { return join("", array_slice($ar[0], $start)); //join()=>implode() } break; default: $start = $start * 2; $length = $length * 2; $strlen = strlen($string); for ($i = 0; $i < $strlen; $i++) { if ($i >= $start && $i < $start + $length) { if (ord(substr($string, $i, 1)) > 129) { $tmpstr .= substr($string, $i, 2); } else { $tmpstr .= substr($string, $i, 1); } } if (ord(substr($string, $i, 1)) > 129) { $i++; } //返回字符的 ASCII 码值 } if (strlen($tmpstr) < $strlen) { $tmpstr .= $dot; } return $tmpstr; } }
/** * Loads HTTP data. * @param array * @return void */ public function loadHttpData($data) { parent::loadHttpData($data); if ($this->control->type === 'password') { $this->tmpValue = ''; } if ($this->control->maxlength && iconv_strlen($this->value, 'UTF-8') > $this->control->maxlength) { $this->value = iconv_substr($this->value, 0, $this->control->maxlength, 'UTF-8'); } }