>因为本博客在文章列表时,需要显示一段内容,但是好多情况是有html标签等,或者是里面带有链接、图片的标签,于是在网上找了很多,发现都不尽人意,就自己写了一个适合自己使用的: /** * 字符串切割+过滤+转换 * * 功能:截取字符串(支持中文) * 如果截取的字符串中不包含html标签,则正常截取 * 如果字符串中包括img标签,则先进行过去标签,截取后,将标签位置放回,截取的字符串则会保留完整的html标签 * * @param string $string * @param unknown $length * @param string $replace * @return string */ function cutTab($string, $length='15', $dot = '…') { $_lenth = mb_strlen($string, "utf-8"); $text_str = preg_replace("//si","",$string); $text_lenth = mb_strlen($text_str, "utf-8") - 1; if($text_lenth <= $length) { return stripcslashes($string); } if(strpos($string, '') / 3); if($length < $html_start) { $res = mb_substr($string, 0, $length, 'UTF-8'); return stripcslashes($res).$dot; } if($length > $html_start) { $res_html = mb_substr($text_str, 0, $length-1, 'UTF-8'); preg_match('/]*\>/',$string,$result_html); $before = mb_substr($res_html, 0, $html_start, 'UTF-8'); $after = mb_substr($res_html, $html_start, mb_strlen($res_html, "utf-8"), 'UTF-8'); $res = $before.$result_html[0].$after; return stripcslashes($res).$dot; } }