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

Source: Internet
Author: User

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

This article describes how to solve the garbled characters intercepted by multiple Chinese and English characters in smarty. 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:
Copy codeThe Code is as follows: <? Php
Function smartDetectUTF8 ($ string)
{
Static $ result = array ();
If (! Array_key_exists ($ key = md5 ($ string), $ result ))
{
$ Utf8 ="
/^ (? :
[\ 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} # 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 ($ utf8), $ string );
}
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) & gt; 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) & gt; 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:
Copy codeThe Code is as follows: {$ content | smartTruncate: 5: ".."} ($ content equals to "A, B, C, D, Min 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.


PHP smarty intercepts Chinese garbled characters

Gbk or UTF-8?
Smarty is not in the main file, but in the plugins directory.
Modifier. truncate. php file
Different smart truncation functions are available based on different codes.

Ask php how to deal with the garbled characters in string Truncation

<? Php $ string = "baking soda is very proud"; $ new_str = mb_substr ($ string, 2, 4, "UTF-8"); // mb_substr. php built-in function for intercepting strings. The parameters are: the string to be intercepted, starting from the nth character, intercepting several characters, and converting character encoding (UTF-8/gbk/big5 ).

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.