/** * Obtain the nth occurrence position of a character in the string. * @ Param string $ text string * @ Param string $ key * @ Param int $ int N * @ Return int */ Function strpos_int ($ text, $ key, $ int) { $ Keylen = strlen ($ key ); Global $ textlen; If (! $ Textlen) $ Textlen = strlen ($ text ); Static $ textpos = 0; $ Pos = strpos ($ text, $ key ); $ Int --; If ($ pos) { If ($ int = 0) $ Textpos + = $ pos; Else $ Textpos + = $ pos + $ keylen; } Else { $ Int = 0; $ Textpos = $ textlen; } If ($ int> 0) { Strpos_int (substr ($ text, $ pos + $ keylen), $ key, $ int ); } Return $ textpos; } /** * Intercept HTML * @ Param string $ string HTML string * @ Param int $ length: the length of the clip. * @ Param string $ dot * @ Param string $ append * @ Return string */ Function cuthtml ($ string, $ length, $ dot = '...', $ append = "") { $ Str = strip_tags ($ string); // filter tags first $ New_str = iconv_substr ($ str, 0, $ length, 'utf-8 '); $ Last = iconv_substr ($ new_str,-1, 1, 'utf-8 '); $ SC = substr_count ($ new_str, $ last ); $ Position = strpos_int ($ string, $ last, $ SC); // Obtain the extract length. If (function_exists ('tidy _ parse_string ') // if tidy is enabled on the server, the html code of the function is incomplete. { $ Options = array ("show-body-only" => true ); Return tidy_parse_string (mb_substr ($ string, 0, $ position). $ dot. $ append, $ options, 'utf8 '); } Else // tidy is not enabled { If (strlen ($ string) <= $ position) { Return $ string; } $ Pre = chr (1 ); $ End = chr (1 ); $ String = str_replace (array ('&', '"', '<', '>'), array ($ pre. '&'. $ end, $ pre. '"'. $ end, $ pre. '<'. $ end, $ pre. '> '. $ end), $ string ); $ Strcut = ''; $ N = $ tn = $ noc = 0; While ($ n <strlen ($ string )) { $ T = ord ($ string [$ n]); If ($ t = 9 | $ t = 10 | (32 <= $ t & $ t <= 126 )) { $ Tn = 1; $ N ++; $ Noc ++; } Elseif (194 <=$ t & $ t <= 223) { $ Tn = 2; $ N + = 2; $ Noc + = 2; } Elseif (224 <=$ t & $ t <= 239) { $ Tn = 3; $ N + = 3; $ Noc + = 2; } Elseif (240 <=$ t & $ t <= 247) { $ Tn = 4; $ N + = 4; $ Noc + = 2; } Elseif (248 <=$ t & $ t <= 251) { $ Tn = 5; $ N + = 5; $ Noc + = 2; } Elseif ($ t = 252 | $ t = 253) { $ Tn = 6; $ N + = 6; $ Noc + = 2; } Else { $ N ++; } If ($ noc >=$ position) { Break; } } If ($ noc> $ position) { $ N-= $ tn; } $ Strcut = substr ($ string, 0, $ n ); $ Strcut = str_replace (array ($ pre. '&'. $ end, $ pre. '"'. $ end, $ pre. '<'. $ end, $ pre. '> '. $ end), array ('&', '"', '<', '>'), $ strcut ); $ Pos = strrpos ($ strcut, chr (1 )); If ($ pos! = False) { $ Strcut = substr ($ strcut, 0, $ pos ); } Return $ strcut. $ dot. $ append; } } |