How does php limit the display length of characters?

Source: Internet
Author: User
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 ;}

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.