Clean HTML

function CLEAN_HTML($html,$remove_images=false){ $html = trim($html); $html = trim(str_replace(array('<![CDATA[',']]>'),'',$html)); //$html = str_replace(array('o ','o ', 'o ', 'o '),array('• '),$html); $html = str_replace(array('Ă','ă', 'â', 'Î', 'î', 'Ș', 'ș', 'Ț', 'ț'),array('A','a','a','I','i', 'S','s', 'T', 't'),$html); //clean tags $html = remove_styles($html); //remove links $html = preg_replace('/<a[^<>]+>|<\/a>/s', '', $html); //remove images if($remove_images) $html = remove_images($html); //clean html $html = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $html); $html = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $html); //remove comments $html = preg_replace("/<!--.*-->/Uis", "", $html); //remove h1 $html = str_replace(array("<h1", "</h1", "<p></p>"), array("<h2","</h2", ""), $html); //remove scripts $html = ereg_replace('<script.*</script>', '', $html); //remove iframe $html = ereg_replace('<iframe.*</iframe>', '', $html); //remove forms $html = ereg_replace('<form.*</form>', '', $html); $html = str_replace('<p> </p>', ' ', $html); //remove   $html = str_replace(' ', ' ', $html); return $html; } function remove_images($text,$remove_links = true){ $tags = array('img'); $arr1 = $arr2 = array(); for($i=0;$i<count($tags);$i++){ $arr1[] = '/<'.$tags[$i].'(.|\s)*?>/'; $arr2[] = ''; } return preg_replace($arr1,$arr2,$text); } function remove_styles($text,$remove_links = true){ if($remove_links) $text = remove_links($text); $tags = array('div', 'p', 'ul', 'li', 'span', 'strong', 'h1', 'h2', 'h3', 'h4', 'font'); $arr1 = $arr2 = array(); for($i=0;$i<count($tags);$i++){ $arr1[] = '/<'.$tags[$i].'(.|\s)*?>/'; $arr2[] = '<'.$tags[$i].'>'; } return preg_replace($arr1,$arr2,$text); } function remove_links($html){ return preg_replace('/<a[^<>]+>|<\/a>/s', '', $html); }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.