function safetags ($text) { #This function uses htmlentities to disble markup, then converts those tags that are allowed and properly formatted back into enabled markup. $loner_tags=array('br', 'hl', 'img', 'p' ); #These are tags that can left unclosed. But it's OK if they are closed. $closed_tags=array('a', 'b', 'center', 'div', 'left', 'li', 'right', 'span', 'ul' ); #These are the ones that have to be closed. $i=0; $pattern=array(); $replace=array(); #convert the entire text to html entities. This takes all tags and quotes and converts them to harmless strings. $text=htmlentities($text); #for all allowed tags, if they have closing tags then enable them. $all_tags=array_merge($loner_tags, $closed_tags); foreach($all_tags as $tag) { $pattern[]='/(?\2'; } #Enable those tags that don't have closing tags and are allowed without them. foreach($loner_tags as $tag) { $pattern[]='/(?'; } #Within each properly converted tag, convert the properly quoted attributes. $pattern[]='/<([^<>]*?)\"(.*?)\"([^<>]*?)>/'; $replace[]='<\1 "\2" \3>'; $pattern[]='/<([^<>]*?)\'(.*?)\'([^<>]*?)>/'; $replace[]='<\1 "\2" \3>'; #Here's where the work gets done for($i=0;$i