_php examples of methods for improving truncate in smarty to support Chinese

Source: Internet
Author: User
Tags modifier ord php template strlen smarty template

The examples in this article describe the ways in which Smarty improved truncate to support Chinese. Share to everyone for your reference, specific as follows:

Smarty's truncate does not support the interception of Chinese. Change the modifier.truncate.php in the plugins in the Smarty directory to the following.

 <?php/* Smarty plugin *--------------------------------------------------------- ----* Type:modifier * name:truncate * purpose:truncate A string to a certain length if necessary, * Optionall
Y splitting in the middle of a word, and * appending the $ETC string.
* Update:change substr to Msubstr and add fun msubstr.so it * can bu used in Chinese charset. *-------------------------------------------------------------/function Smarty_modifier_truncate ($string, $
  Length = n, $etc = ' ... ', $break _words = False) {if ($length = = 0) return ';
    if (strlen ($string) > $length) {$length-= strlen ($ETC); if (! $break _words) $string = Preg_replace ('//s+?
    /s+)? $/', ', Substrgb ($string, 0, $length + 1));
  Return Substrgb ($string, 0, $length). $etc;
else return $string; function Substrgb ($str, $start, $len) {for ($i =0; $i < $start + $len; $i + +) {$tmpstr = (ord ($str [$i]) >=161 && o RD ($str [$i])<=254&& Ord ($str [$i +1]) >=161 && ord ($str [$i +1]) <=254)? $str [$i]. $str [+ + $i]: $tmpstr = $str [$
    I];
  if ($i >= $start && $i < ($start + $len)) $tmp. = $tmpstr;
return $tmp; }/* Vim:set expandtab:/*?> 

More interested in smarty related content readers can view the site topics: "Smarty Template Primer Tutorial", "PHP Template Technology Summary", "PHP based on PDO Operation Database Skills summary", "PHP Operations and Operator Usage Summary", "PHP Network Programming Skills Summary", " Introduction to PHP Basic Grammar, "Introduction to PHP object-oriented programming", "PHP string (String) Usage Summary", "Php+mysql Database Operations Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on Smarty template.

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.