Smarty use truncate to intercept Chinese and English strings and avoid the problem of garbled characters

Source: Internet
Author: User
Tags modifier ord strlen utf 8

Smarty in the use of truncate to intercept the string containing Chinese and English, there may be garbled problems, the length of string interception, the following is a new extension function, or modify the original Truncate function method can also be.

Expand the Smarty/plugins directory to create a new file, write a function,
Modified smarttruncate: File name: modifier.smartTruncate.php content is as follows:

<?php function smarty_modifier_smarttruncate ($string,  $length  = 80,  $etc  =  ' ... ', $break _words = false,  $middle  = false)  {      if  ($length  == 0)          return  ';     if  (Smartstrlen ($string)  >  $length)  {  
       $length  -= smartstrlen ($ETC);
        if  (! $break _words && ! $middle)  {              $string  = preg_replace ('/\s+ ?
(\s+) $/',  ',  smartsubstr ($string, 0,  $length + 1)); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}         if (! $middle)  {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;SMArtsubstr ($string, 0,  $length). $etc;         } else {             return smartsubstr ($string, 0,  $length/2)  .  $etc  .
 smartsubstr ($string,  -$length/2); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}     } else {   
     return  $string; &NBSP;&NBSP;&NBSP;&NBSP} Function smartdetectutf8 ($string) {    static  $result
 = array ();     if (! array_key_exists ($key  = md5 ($string),  $result))      {         $utf 8 =  "             /^ (?:                  [\x09\x0a\x0d\x20-\x7e]                             # ascii                  | [\xC2-\xDF][\x80-\xBF]                               # non-overlong 2-byte                  | \xE0[\xA0-\xBF][\x80-\xBF]                         # excluding overlongs                  | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}          &nbsP; # straight 3-byte                  | \xED[\x80-\x9F][\x80-\xBF]                       # excluding  Surrogates                 |  \xF0[\x90-\xBF][\x80-\xBF]{2}                  # planes 1-3                  | [\xF1-\xF3][\x80-\xBF]{3}                            # planes 4-15                  | \xf4[\x80-\x8f][\x80-\xbf]{2}                   # plane 16           
  ) +$/xs          ";          $result [$key] = preg_match (Trim ($utf 8),  $string
);
&NBSP;&NBSP;&NBSP;&NBSP}     return  $result [$key]; } function smartstrlen ($string) {     $result  = 0;     $
Number = smartdetectutf8 ($string)  ? 3 : 2;     for ($i  = 0;  $i  < strlen ($string);  $i  += $ bytes)      {          $bytes  = 
Ord (substr ($string,  $i,  1))  > 127 ?  $number  : 1;          $result  +=  $bytes  > 1 ? 1.0 : 0.5;
         return  $result; } function smartsubstr ($string,  $start,  $length  = null) {    $
result =  ';
     $number  = smartdetectutf8 ($string)  ? 3 : 2;     if ($start  < 0)     {     
    $start  = max (Smartstrlen ($string)  +  $start,  0); &NBSP;&NBSP;&NBSP;&NBSP}     for ($i  = 0;  $i  < strlen ($string);   $i  +=  $bytes)     {        if ($ start <= 0)          {              break;         }          $bytes  = ord (
substr ($string,  $i,  1))  > 127 ?  $number  : 1;          $start  -=  $bytes  > 1 ? 1.0
 : 0.5; &NBSP;&NBSP;&NBSP;&NBSP}     if (Is_null ($length))     {  
       $result  = substr ($string,  $i); &NBSP;&NBSP;&NBSP;&NBSP}     else     {     
   for ($j  =  $i;  $j  < strlen ($string);  $j  +=  $bytes)         {             if ($length  <= 0)               {                 break;              }              if ($bytes  = ord (substr ($string,  $j,  1))  > 127 ? $ number : 1)              > 1)  {                if ($ length < 1.0)                  {                
    break;                 }                   $result &NBSP;.=&NBSp;substr ($string,  $j,  $bytes);                  $length  -
= 1.0;             }              else              {                 $result
 .= substr ($string,  $j,  1);                  $length  -
= 0.5;             }       
       }     return  $result; }

2, the above code to achieve the full truncate of the original function, but also compatible with GB2312 and UTF-8 encoding, in the judgement of the length of characters, a Chinese character is 1.0, an English character is 0.5, so when the substring will not appear uneven.
There's nothing special about the way plug-ins are used, but here's a simple test:

{$content |smarttruncate:5: "..."} ($content equals "u chen N yu v Heng s bo C guest n")

Show: U chen N-yu v. (The length of the Chinese symbol is 1.0, the English symbol length is 0.5, and the length of the elliptical symbol is considered)

{

$string |smarttruncate: "10″:" ... "}

The first is the length of the intercepted character and the second is the suffix character
3. Other issues

Error: (secure mode) modifier Smarttruncate is not allowed

This is primarily because you enabled the Smarty $security function.

Workaround:

Adding allowed functions to the Smarty configuration file

$smarty->security_settings[' modifier_funcs '] = Array (' smarttruncate ');

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.