This method works by intercepting a string containing an HTML tag and closing the tag if it is not closed to prevent the open label from breaking the original page layout. After the interception, replace the extra content with the ... or any other character that supports an anchor point designation to be replaced to the specified location.
* * ============================== intercepts strings containing HTML tags ========================= * @param (String) $str to intercept the string * @param (i NT) $lenth Intercept length * @param (string) $repalce the excess content replaced with $repalce (this parameter can be a string with HTML tags) * @param (String) $anchor intercept the anchor point, if the interception process To this tag anchor, the return value * @demo $res = cut_html_str ($STR, 256, ' ... ') at the anchor point * @return (String) $result Intercept 256 lengths, and replace the remainder with ' ... ' *-------------------------------------------------------------------------------* $ Author:wang Jian. | email:wj@yurendu.com | DATE:2014/03/16 * ===============================================================================/function Cut_ Html_str ($str, $lenth, $replace = ', $anchor = ' <!--break--> ') {$_lenth = Mb_strlen ($str, "utf-8");//Statistics string length (Chinese, English is counted as a character) if ($_lenth <= $lenth) {return $str;//The passed-in string length is less than intercept length, returned as is $strlen _var = strlen ($STR); Statistic string Length (UTF8 code-Chinese 3 characters, English count a character) if (Strpos ($str, ' < ') = = False) {return mb_substr ($str, 0, $lenth);//No HTML included tag, Direct intercept} if ($e = Strpos ($str, $anchOR) {return mb_substr ($str, 0, $e);//include truncation flag, priority} $html _tag = 0; HTML code tag $result = '; Summary string $html _array = Array (' Left ' => array (), ' right ' => array ()); Record the HTML tags that appear in the string after the interception, start =>left, end =>right/* * such as the string: