As shown in the preceding figure, when the character length exceeds the fixed pixel width, the string within the pixel width range is intercepted and the ellipsis is added after the string. The mb_strlen function can only obtain the number of characters, but the width of the English and Chinese characters is different. It cannot be determined based on the number of characters, no...
As shown in the preceding figure, when the character length exceeds the fixed pixel width, the string within the pixel width range is intercepted and the ellipsis is added after the string. The mb_strlen function can only obtain the number of characters, but the width of the English and Chinese characters is different. It cannot be determined based on the number of characters. I don't know how to implement this function?
Reply content:
As shown in the preceding figure, when the character length exceeds the fixed pixel width, the string within the pixel width range is intercepted and the ellipsis is added after the string. The mb_strlen function can only obtain the number of characters, but the width of the English and Chinese characters is different. It cannot be determined based on the number of characters. I don't know how to implement this function?
That can be controlled through CSS... Http://jsfiddle.net/sc7uk5bw/
// Capture the string function msubstr ($ str, $ start = 0, $ length, $ charset = "UTF-8", $ suffix = true) {if (function_exists ("mb_substr ")) {if ($ suffix) {return mb_substr ($ str, $ start, $ length, $ charset);} else {return mb_substr ($ str, $ start, $ length, $ charset) ;}} elseif (function_exists ('iconv _ substr') {if ($ suffix) {return iconv_substr ($ str, $ start, $ length, $ charset);} else {return iconv_substr ($ str, $ start, $ length, $ charset );}} $ re ['utf-8'] = "/[\ x01-\ x7f] | [\ xc2-\ xdf] [\ x80-\ xbf] | [\ xe0 -\ xef] [\ x80-\ xbf] {2} | [\ xf0-\ xff] [\ x80-\ xbf] {3 }/"; $ re ['gb2312'] = "/[\ x01-\ x7f] | [\ xb0-\ xf7] [\ xa0-\ xfe]/"; $ re ['gbk'] = "/[\ x01-\ x7f] | [\ x81-\ xfe] [\ x40-\ xfe]/"; $ re ['big5'] = "/[\ x01-\ x7f] | [\ x81-\ xfe] ([\ x40-\ x7e] | \ xa1-\ xfe]) /"; preg_match_all ($ re [$ charset], $ str, $ match); $ slice = join (" ", array_slice ($ match [0], $ start, $ length); if ($ suffix) {return $ slice;} else {return $ slice ;}}
Give me a method that I usually use to intercept strings. It supports both Chinese and English.
php
// Truncate the public static function mix_substr ($ str, $ len = 12, $ dot = true) {$ I = 0; $ l = 0; $ c = 0; $ a = array (); while ($ l <$ len) {$ t = substr ($ str, $ I, 1); if (ord ($ t)> = 224) {$ c = 3; $ t = substr ($ str, $ I, $ c); $ l + = 2;} elseif (ord ($ t)> = 192) {$ c = 2; $ t = substr ($ str, $ I, $ c); $ l + = 2;} else {$ c = 1; $ l ++;} // $ t = substr ($ str, $ I, $ c); $ I + = $ c; if ($ l> $ len) break; $ a [] = $ t;} $ re = Implode ('', $ a); if (substr ($ str, $ I, 1 )! = False) {array_pop ($ a); ($ c = 1) and array_pop ($ a); $ re = implode ('', $ ); $ dot and $ re. = '... ';} return $ re ;}