以下是php中的函数,它接受任何字符串(字符串也包含html标签),并从变量$ min中提到的字符中返回几个字。如何从HTML标签读取CDATA
function gen_string($string,$min=500,$clean=true) {
$text = trim(strip_tags($string));
if(strlen($text)>$min) {
$blank = strpos($text,' ');
if($blank) {
# limit plus last word
$extra = strpos(substr($text,$min),' ');
$max = $min+$extra;
$r = substr($text,0,$max);
$query = "select distinct ID from cms_content";
$result = mysql_query($query);
$IDlink = 'http://localhost/www/index.php?ID='.$result;
if(strlen($text)>=$max && !$clean) $r=trim($r,'.') ;
} else {
# if there are no spaces
$r = substr($text,0,$min).'.........';
}
} else {
# if original length is lower than limit
$r = $text;
}
return trim($r);
}
但问题是,在返回的字符串中,它不会读取html标记。 那么如何让这个函数读取html标签,以便返回的字符串必须在格式化的html标签中?
不错的问题,+1 – Flavius 2012-01-01 08:28:59