Describes a built-in Chinese/English string truncation function in Thinkphp.

Source: Internet
Author: User

Thinkphp has a built-in template engine comparable to smarty, which brings us great convenience. The same is true for calling functions. You can call the functions you need like smarty, and some common functions are built into the official system for you to call.

For example, the string truncation function we are talking about today can be written in the thinkphp template engine as follows: {$ vo. title | msubstr =, 'utf-8', false} As to {$ vo. title. Let's talk about the msubstr function. It indicates that the string $ vo. title is truncated, starting from 0 characters and intercepting 5 characters. UTF-8 encoding is used. The ellipsis is not displayed after the truncation by default. If you want to display the ellipsis, simply change false to true.

Function explanation:

Msubstr ($ str, $ start = 0, $ length, $ charset = "UTF-8", $ suffix = true)

  • $ Str: string to be truncated
  • $ Start = 0: start position, starting from 0 by default
  • $ Length: truncation length
  • $ Charset = "UTF-8": character encoding, default UTF-8
  • $ Suffix = true: whether the ellipsis is displayed after the truncated characters. The default value is true, and false indicates that the ellipsis is not displayed.

Ps: if it cannot be called normally, it means that you have not loaded the function library. You can use Load ('extend'); to Load the function and put it in action ~!

Trial: the official msubstr function does not seem to be able to add the ellipsis in any case. I found a modification method on the official website forum. It can be used normally after testing ~!

Modify the msubstr function of the Commonextend. php file to the following code:

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][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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.