This article describes how to solve the problem of smarty multi-encoding Chinese and English character truncation garbled characters. It involves modifying the original smartTruncate, which is of great practical value. For more information, see
This article describes how to solve the problem of smarty multi-encoding Chinese and English character truncation garbled characters. It involves modifying the original smartTruncate, which is of great practical value. For more information, see
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:
The 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:
The Code is as follows: