/** * Interception of HTML, and automatic completion of the full closure * @param $html * @param $length * @param $end */ function subhtml ($html, $length) { $result = '; $tagStack = Array (); $len = 0; $contents = Preg_split ("~ (<[^>]+?>) ~si", $html, -1,preg_split_no_empty| Preg_split_delim_capture); foreach ($contents as $tag) { if (Trim ($tag) = = "") Continue if (Preg_match ("~<" ([a-z0-9]+) [^/>]*?/>~si, $tag)) { $result. = $tag; }else if (Preg_match ("~</" ([a-z0-9]+) [^/>]*?>~si, $tag, $match)) { if ($tagStack [count ($tagStack)-1] = = $match [1]) { Array_pop ($tagStack); $result. = $tag; } }else if (Preg_match ("~<" ([a-z0-9]+) [^/>]*?>~si, $tag, $match)) { Array_push ($tagStack, $match [1]); $result. = $tag; }else if (Preg_match ("~<!--. *?-->~si", $tag)) { $result. = $tag; }else{ if ($len + mstrlen ($tag) < $length) { $result. = $tag; $len + + Mstrlen ($tag); }else { $str = msubstr ($tag, 0, $length-$len + 1); $result. = $str; Break } } } while (!empty ($tagStack)) { $result. = ' </'. Array_pop ($tagStack). ' > '; } return $result; } /** * Intercept Chinese strings * @param $string string * @param $start starting position * @param $length length * @param $charset Coding * @param $dot Additional strings */ function Msubstr ($string, $start, $length, $dot = ', $charset = ' UTF-8 ') { $string = Str_replace (' & ', ' " ', ' < ', ' > ', ' '), Array (' & ', ' "', ', ', ' < ', ' > ', '), $string); if (strlen ($string) <= $length) { return $string; } if (Strtolower ($charset) = = ' Utf-8 ') { $n = $tn = $noc = 0; while ($n < strlen ($string)) { $t = Ord ($string [$n]); if ($t = = 9 | | $t = 10 | | (<= $t && $t <= 126)) { $tn = 1; $n + +; } elseif (194 <= $t && $t <= 223) { $tn = 2; $n + 2; } elseif (224 <= $t && $t <= 239) { $tn = 3; $n + 3; ElseIf (<= $t && $t <= 247) { $tn = 4; $n + 4; } elseif (248 <= $t && $t <= 251) { $tn = 5; $n + 5; } elseif ($t = = 252 | | $t = = 253) { $tn = 6; $n + 6; } else { $n + +; } $noc + +; if ($noc >= $length) { Break } } if ($noc > $length) { $n-= $tn; } $strcut = substr ($string, 0, $n); } else { for ($i = 0; $i < $length; $i + +) { $strcut. = Ord ($string [$i]) > 127? $string [$i]. $string [+ + $i]: $string [$i]; } } return $strcut. $dot; } /** * Get the length of the string, including Chinese and English. */ function Mstrlen ($str, $charset = ' UTF-8 ') { if (function_exists (' mb_substr ')) { $length =mb_strlen ($str, $charset); } elseif (Function_exists (' iconv_substr ')) { $length =iconv_strlen ($str, $charset); } else { 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]/", $text, $ar); $length =count ($ar [0]); } return $length; } |