Solution to smarty multi-encoding Chinese and English character truncation garbled characters

Source: Internet
Author: User
This article describes how to solve the garbled characters of smarty multi-encoding Chinese and English characters. it involves modifying the original smartTruncate, which is of great practical value, for more information, see the example in this article. the solution to the problem of smarty multi-encoding Chinese/English character truncation and garbled characters is provided. The specific method is as follows:

Generally, the display of website pages inevitably involves the interception of sub-strings. at this time, truncate is useful, but it is only suitable for English users. for Chinese users, truncate may cause garbled characters. for Chinese and English strings with the same number of characters, the actual display length is different, which may be visually uneven and affect the appearance. This is because the length of a Chinese character is roughly equivalent to the length of two English letters. In addition, truncate is not compatible with GB2312, UTF-8 and other encoding.
Improved smartTruncate: File name: modifier. smartTruncate. php
The code is as follows:

  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);     }    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;     }    if(is_null($length))     {         $result = substr($string, $i);     }     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 .= substr($string, $j, $bytes);                 $length -= 1.0;             }             else             {                 $result .= substr($string, $j, 1);                 $length -= 0.5;             }         }     }    return $result; }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));         }         if(!$middle) {             return smartSubstr($string, 0, $length).$etc;         } else {             return smartSubstr($string, 0, $length/2) . $etc . smartSubstr($string, -$length/2);         }     } else {         return $string;     } } ?>


The above code fully implements the original functions of truncate, and can be compatible with GB2312 and UTF-8 encoding at the same time, when determining the length of a Chinese character is 1.0, an English character is 0.5, therefore, when the substring is intercepted, there will be no variation.
The usage of the plug-in is not special. here is a simple test:

The code is as follows:

{$ Content | smartTruncate: 5: ".."} ($ content is equal to "A, B, C, D, E, F, and G, H ")


Display: B Hua C .. in A (the length of Chinese characters is 1.0, the length of English characters is 0.5, and the length of Chinese characters is ignored)
Whether you are using GB2312 or UTF-8 encoding, you will find that the results are correct, which is one of the reasons why I add the smart words to the plug-in name.

I hope this article will help you with PHP programming.

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.