Add the following code to the Common directory in the project directory. php files, such as www/Common/common. php file, of course, you can also directly add it to the Common/common of thinkphp. in the php file, all projects can be used.
| The code is as follows: |
Copy code |
| Function truncate_cn ($ string, $ length = 0, $ ellipsis = '... ', $ Start = 0 ){ $ String = strip_tags ($ string ); $ String = preg_replace ('/\ n/is', '', $ string ); // $ String = preg_replace ('/|/is', ', $ string); // clear spaces in the string $ String = preg_replace ('/& nbsp;/is', '', $ string ); Preg_match_all ("/[\ x01-\ x7f] | [\ xc2-\ xdf] [\ x80-\ xbf] | \ xe0 [\ xa0-\ xbf] [\ x80- \ xbf] | [\ xe1-\ xef] [\ x80-\ xbf] [\ x80-\ xbf] | \ xf0 [\ x90-\ xbf] [\ x80 -\ xbf] [\ x80-\ xbf] | [\ xf1-\ xf7] [\ x80-\ xbf] [\ x80-\ xbf] [\ x80-\ xbf]/", $ string, $ string ); If (is_array ($ string )&&! Empty ($ string [0]) { $ String = implode ('', $ string [0]); If (strlen ($ string) <$ start + 1 ){ Return ''; } Preg_match_all ("/./su", $ string, $ ar ); $ String2 = ''; $ Tstr = ''; // Www.111cn.net For ($ I = 0; isset ($ ar [0] [$ I]); $ I ++ ){ If (strlen ($ tstr) <$ start ){ $ Tstr. = $ ar [0] [$ I]; } Else { If (strlen ($ string2) <$ length + strlen ($ ar [0] [$ I]) { $ String2. = $ ar [0] [$ I]; } Else { Break; } } } Return $ string = $ string2? $ String2: $ string2. $ ellipsis; } Else { $ String = ''; } Return $ string; } |
The thinkphp template is used as follows:
| The code is as follows: |
Copy code |
| {$ Info. subject | truncate_cn = 40, '', 0} |
It means to intercept the $ info ['subobject'] string. The string with a length of 40 is truncated starting from 0th. If the length of the truncated string is smaller than the length of the original string, nothing is displayed, the default value is... in fact, the following two parameters are optional.