/** * 截取字符串,用...代替 */ function sub_str($str, $number) { if (strlen_utf8($str) > $number) { return substr_utf8($str, 0, $number) . '...'; } return $str; }
public function submitmsg() { Load('extend'); if ($_SESSION['verify'] != md5($_POST['verify'])) { $this->error('验证码错误!'); } elseif (strlen_utf8($_POST['content']) > 200 || strlen_utf8($_POST['content']) < 10) { $this->error("留言内容不能少于10字,不能超过200字!"); } else { $data = $_POST; $Message = D("Message"); $data['clientip'] = get_client_ip(); $data['postdate'] = time(); if ($Message->Create()) { if ($Message->add($data)) { $this->assign("jumpUrl", "__URL__"); $this->success("留言成功!"); } else { $this->error("留言失败!"); } } else { $this->error($Message->getError()); } } }
/** +---------------------------------------------------------- * 字符串截取,支持中文和其他编码 +---------------------------------------------------------- * @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 = true) { $str = strip_tags($str, ''); // Fengzi change at 2010.1.30 if (is_utf8($str) && strlen_utf8($str) <= $length) { return $str; } // Fengzi change at 2010.3.25 if (function_exists("mb_substr")) { return mb_substr($str, $start, $length, $charset) . "..."; } elseif (function_exists('iconv_substr')) { 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; }
function truncate($text, $length = 200, $ending = '...', $exact = true, $considerHtml = true) { if ($considerHtml) { // if the plain text is shorter than the maximum length, return the whole text if (strlen_utf8(preg_replace('/<.*?>/u', '', $text)) <= $length) { return $text; } // splits all html-tags to scanable lines preg_match_all('/(<.+?>)?([^<>]*)/su', $text, $lines, PREG_SET_ORDER); $total_length = strlen_utf8($ending); $open_tags = array(); $truncate = ''; foreach ($lines as $line_matchings) { // if there is any html-tag in this line, handle it and add it (uncounted) to the output if (!empty($line_matchings[1])) { // if it's an "empty element" with or without xhtml-conform closing slash (f.e. <br/>) if (preg_match('/^<(\\s*.+?\\/\\s*|\\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\\s.+?)?)>$/isu', $line_matchings[1])) { // do nothing } else { if (preg_match('/^<\\s*\\/([^\\s]+?)\\s*>$/us', $line_matchings[1], $tag_matchings)) { // delete tag from $open_tags list $pos = array_search($tag_matchings[1], $open_tags); if ($pos !== false) { unset($open_tags[$pos]); } } else { if (preg_match('/^<\\s*([^\\s>!]+).*?>$/su', $line_matchings[1], $tag_matchings)) { // add tag to the beginning of $open_tags list array_unshift($open_tags, strtolower($tag_matchings[1])); } } } // add html-tag to $truncate'd text $truncate .= $line_matchings[1]; } // calculate the length of the plain text part of the line; handle entities as one character $content_length = strlen_utf8(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/ui', ' ', $line_matchings[2])); if ($total_length + $content_length > $length) { // the number of characters which are left $left = $length - $total_length; $entities_length = 0; // search for html entities if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/iu', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) { // calculate the real length of all entities in the legal range foreach ($entities[0] as $entity) { if ($entity[1] + 1 - $entities_length <= $left) { $left--; $entities_length += strlen_utf8($entity[0]); } else { // no more characters left break; } } } $truncate .= substr_utf8($line_matchings[2], 0, $left + $entities_length); // maximum lenght is reached, so get off the loop break; } else { $truncate .= $line_matchings[2]; $total_length += $content_length; } // if the maximum length is reached, get off the loop if ($total_length >= $length) { break; } } } else { if (strlen_utf8($text) <= $length) { return $text; } else { $truncate = substr_utf8($text, 0, $length - strlen_utf8($ending)); } } // if the words shouldn't be cut in the middle... if (!$exact) { // ...search the last occurance of a space... $spacepos = strrpos($truncate, ' '); if (isset($spacepos)) { // ...and cut the text in this position $truncate = substr_utf8($truncate, 0, $spacepos); } } // add the defined ending to the text $truncate .= $ending; if ($considerHtml) { // close all unclosed html-tags foreach ($open_tags as $tag) { $truncate .= '</' . $tag . '>'; } } return $truncate; }
// OUTPUT JSON header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Pragma: no-cache"); // HTTP/1.0 header("Content-Type: application/json"); echo json_encode(array('user_exists' => (bool) $user_exists)); exit; } elseif ($task == "suggest_field") { // GET USER INPUT AND LIMIT $input = strtolower($_GET['input']); $len = strlen_utf8($input); $limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 20; // GET OPTIONS FROM URL $results = array(); $options = is_array($_POST['options']) ? $_POST['options'] : (is_array($_GET['options']) ? $_GET['options'] : array()); foreach ($options as $option_index => $option_value) { if (strtolower(substr($option_value, 0, $len)) == $input) { $results[] = array("id" => NULL, "value" => $option_value, "info" => $option_value, "photo" => NULL, "photo_width" => NULL); } } // OUTPUT JSON header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified header("Cache-Control: no-cache, must-revalidate");