龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

strip_tags延伸函数-处理html(采集用的上)

时间:2014-07-22 14:49来源: 作者: 点击:
分享到:
函数收集整理于互联网 br / br / br / 更多函数 a href=http://www.phpno.com target=_blank rel=nofollowphp/a博客:http://www.phpno.com/strip_tags_extend.html br / br /
函数收集整理于互联网


更多函数 php博客:http://www.phpno.com/strip_tags_extend.html


/**
 * This function turns HTML into text
 * 将html转化为txt 
 */
function html2txt($document) {
	$search = array ('@<script[^>]*?>.*?</script>@si', // Strip out javascript
					'@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
					'@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
					'@<![\s\S]*?--[ \t\n\r]*>@' )// Strip multi-line comments including CDATA
	;
	$text = preg_replace ( $search, '', $document );
	return $text;
}

/**
 * removes the HTML tags along with their contents
 * 移除/过滤html标签并且移除标签内的内容
 * 注:$tags为需要保留的标签  invert为true时结果相反
 */
function strip_tags_content($text, $tags = '', $invert = FALSE) {
	preg_match_all ( '/<(.+?)[\s]*\/?[\s]*>/si', trim ( $tags ), $tags );
	$tags = array_unique ( $tags [1] );
	
	if (is_array ( $tags ) and count ( $tags ) > 0) {
		if ($invert == FALSE) {
			return preg_replace ( '@<(?!(?:' . implode ( '|', $tags ) . ')\b)(\w+)\b.*?>.*?</\1>@si', '', $text );
		} else {
			return preg_replace ( '@<(' . implode ( '|', $tags ) . ')\b.*?>.*?</\1>@si', '', $text );
		}
	} elseif ($invert == FALSE) {
		return preg_replace ( '@<(\w+)\b.*?>.*?</\1>@si', '', $text );
	}
	return $text;
}
精彩图集

赞助商链接