Chinese truncation function
The code is as follows: |
Copy code |
Function utf8_trim ($ str) { $ Len = strlen ($ str ); For ($ I = strlen ($ str)-1; $ I >=0; $ I-= 1 ){ $ Hex. = ''. ord ($ str [$ I]); $ Ch = ord ($ str [$ I]); If ($ ch & 128) = 0) return (substr ($ str, 0, $ I )); If ($ ch & 192) = 192) return (substr ($ str, 0, $ I )); } Return ($ str. $ hex ); } Function mul_excerpt ($ excerpt, $ excerpt_length ){ $ Myexcerpt = substr ($ excerpt, 0, $ excerpt_length ); Return utf8_trim ($ myexcerpt ).'... '; } Alternatively // Automatically truncation without garbled characters // Jieduan luanma ///////////////// Function Limit_Char ($ max_char = 200, $ more_text = '...', $ more_link_text = '', $ limit_type = 'content '){ If ($ limit_type = 'TITLE') {$ limiter = get_the_title ();} Else {$ limiter = get_the_content ();} $ Limiter = apply_filters ('The _ content', $ limiter ); $ Limiter = strip_tags (str_replace (']>', ']>', $ limiter )); If (strlen ($ limiter)> $ max_char ){ $ Limiter = substr ($ limiter, 0, $ max_char + 1 ); $ Limiter = utf8_conver ($ limiter ); Echo $ limiter; Echo $ more_text; If ($ more_link_text! = ''){ Echo ''. $ more_link_text .''; } } Else { Echo $ limiter; } } Function utf8_conver ($ str ){ $ Len = strlen ($ str ); For ($ I = strlen ($ str)-1; $ I >=0; $ I-= 1 ){ $ Hex. = ''. ord ($ str [$ I]); $ Ch = ord ($ str [$ I]); If ($ ch & 128) = 0) return (substr ($ str, 0, $ I )); If ($ ch & 192) = 192) return (substr ($ str, 0, $ I )); } Return ($ str. $ hex ); } |
Extract Chinese titles
The code is as follows: |
Copy code |
Echo '</p> <p> '. mb_substr (get_the_title (), 0, 18, "utf8 "). "... ". '</p> </li> '; |
This will lead to chips.
I added the Chinese screenshot code above to functions. php.
Then, add get_short_title () to the position where the title is to be captured to achieve the goal. The detailed code is as follows:
The code is as follows: |
Copy code |
Echo '<li> <a href = "'. get_permalink (). '"> '; Get_short_title (18 ); Echo '</a> <data>'. get_the_time ('N months j ').' </data> </li> '; |
Of course, you can call it in other places like this:
The code is as follows: |
Copy code |
<? Php get_short_title ();?> |