代码如下 | 复制代码 |
/** $contents = preg_split("~(<[^>]+?>)~si",$html, -1,PREG_SPLIT_NO_EMPTY| PREG_SPLIT_DELIM_CAPTURE); } /** if(strtolower($charset) == 'utf-8') { return $strcut.$dot; /** |
实例
代码如下 | 复制代码 |
* @param 要截取的HTML $str if(ord($str[$i])>128) if ($str[$i+1]=='/') for(;$i<$leng;$i++) |
PHP截取字符串,生成文章摘要
我们在写BLOG时经常需要显示文章前一部分,但是又怕不恰当截断破坏封闭标签以造成整个文档结构破坏
代码如下 | 复制代码 |
function text_zhaiyao($text,$length){ //文章摘要生成函数 $test:内容 $length:摘要长度 global $Briefing_Length; mb_regex_encoding("UTF-8"); if(mb_strlen($text) <= $length ) return $text; $Foremost = mb_substr($text, 0, $length); $re = "<(/?) (P|DIV|H1|H2|H3|H4|H5|H6|ADDRESS|PRE|TABLE|TR|TD|TH|INPUT|SELECT|TEXTAREA|OBJECT|A|UL|OL|LI| BASE|META|LINK|HR|BR|PARAM|IMG|AREA|INPUT|SPAN)[^>]*(>?)"; $Single = "/BASE|META|LINK|HR|BR|PARAM|IMG|AREA|INPUT|BR/i"; $Stack = array(); $posStack = array(); mb_ereg_search_init($Foremost, $re, 'i'); while($pos = mb_ereg_search_pos()){ $match = mb_ereg_search_getregs(); /* [Child-matching Formulation]: $matche[1] : A "/" charactor indicating whether current "<...>" Friction is Closing Part $matche[2] : Element Name. $matche[3] : Right > of a "<...>" Friction */ if($match[1]==""){ $Elem = $match[2]; if(mb_eregi($Single, $Elem) && $match[3] !=""){ continue; } |