Phpsmarty intercepts Chinese characters and garbled characters? Gb2312utf-8. 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, 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, if you use truncate, garbled characters may occur. In addition, for Chinese and English strings with the same number of characters, the actual display length varies, and the image looks uneven visually. 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:
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:
{$ 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.
...