Smarty template Chinese string truncation plug-in

Source: Internet
Author: User
For example, the Smarty template uses the string truncation function modifier. truncate. php. this truncate function can intercept English strings, which are commonly used. After all, Smarty is developed by foreigners. if it is used directly to intercept Chinese characters, you will be waiting for garbled characters. In addition, the length values written during truncation are subject to letters, that is, the length of a Chinese character is 2 (GB

For example, the Smarty template uses the string truncation function modifier. truncate. php. this truncate function can intercept English strings, which are commonly used. After all, Smarty is developed by foreigners. if it is used directly to intercept Chinese characters, you will be waiting for garbled characters. Also, the length value written during the truncation is based on letters, that is, the length of a Chinese character is 2 (GBK) or 3 (UTF-8) bytes. Therefore, the following plug-ins can be used for Chinese characters.
This screenshot function comes from the string class in ThinkPHP and is quite useful. What we need to do is to make this function a Smarty plug-in for direct use in the template. OK.
Open the Smartyplugins folder, which is a default plug-in. Here we can see this modifier. truncate. php. Next, we will create a php file in this directory to intercept Chinese strings. If the function name is used, use the default value in TP. Create the file modifier. msubstr. php. the code is as follows:

 
 
  1. /**
  2. * Smarty Chinese string truncation
  3. * Tianya PHP blog Co., http://blog.phpha.com.
  4. */
  5. Function smarty_modifier_msubstr ($ str, $ start = 0, $ length, $ charset = "UTF-8", $ suffix = true ){
  6. If (function_exists ("mb_substr ")){
  7. Return mb_substr ($ str, $ start, $ length, $ charset );
  8. } Elseif (function_exists ('iconv _ substr ')){
  9. Return iconv_substr ($ str, $ start, $ length, $ charset );
  10. }
  11. $ Re ['utf-8'] = "/[x01-x7f] | [xc2-xdf] [x80-xbf] | [xe0-xef] [x80-xbf] {2} | [xf0-xff] [x80-xbf] {3 }/";
  12. $ Re ['gb2312'] = "/[x01-x7f] | [xb0-xf7] [xa0-xfe]/";
  13. $ Re ['gbk'] = "/[x01-x7f] | [x81-xfe] [x40-xfe]/";
  14. $ Re ['big5'] = "/[x01-x7f] | [x81-xfe] ([x40-x7e] | xa1-xfe])/";
  15. Preg_match_all ($ re [$ charset], $ str, $ match );
  16. $ Slice = join ("", array_slice ($ match [0], $ start, $ length ));
  17. If ($ suffix) return $ slice .'...';
  18. Return $ slice;
  19. }
  20. ?>

The following describes the use of the template. For example, if you want to extract the length of the homepage news title, the variable name is title, as follows:

 
 
  1. <{$title|msubstr:0:5}>

It indicates that, starting from the first character, the truncation length is 5, that is, 5 Chinese characters. Here, this function is processed. a letter has the same length as a Chinese character and is 1.

 
 
  1. $ Title = 'Welcome to the blog of Tianya ';
  2. // The result is "Welcome to Heaven"
  3. $ Title = 'tianya PHP blog ';
  4. // The result is "Tianya PHP"

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.