src/HTML.php
private static function _cleanHTML(DOMNode $Node, array $filters){

		$node_contents = '';
		$tag_name = '';

		if(isset($Node->tagName)){

			$tag_name = strtolower(trim($Node->tagName));
		}

		// Drop tags

		assert('isset($filters[\'tags_drop\']) && is_array($filters[\'tags_drop\'])');
		if(!isset($filters['tags_drop']) || !is_array($filters['tags_drop'])){

			$filters['tags_drop'] = [];
		}

		if($Node instanceof DOMComment || in_array($tag_name, $filters['tags_drop'])){

			return '';
		}

		// Clean node contents

		if(!isset($Node->childNodes) || $Node->childNodes->length == 0){

			// As no charset conversion is needed, nodeValue can be directly used

			$node_contents = html_entity_decode($Node->nodeValue, ENT_QUOTES, DEFAULT_CHARSET);

			// Reduce every possible combination of whitespaces to a single space-character

			$node_contents = preg_replace('/\s+/', ' ', $node_contents);

			// Reapply HTML entities

			$node_contents = htmlentities($node_contents, ENT_QUOTES, DEFAULT_CHARSET);
		}
		else{

			foreach($Node->childNodes as $Child){

				$node_contents .= self::_cleanHTML($Child, $filters);
			}
		}

		// Nameless and disallowed nodes (return content only, no tags)

		assert('isset($filters[\'tags\']) && is_array($filters[\'tags\'])');
		if(!isset($filters['tags']) || !is_array($filters['tags'])){

			$filters['tags'] = [];
		}

		if($tag_name == '' || !in_array($tag_name, $filters['tags'])){

			return $node_contents;
		}

		// Allowed nodes (return tag and content)

		else{

			$node_attributes = self::_cleanAttributes($Node, $filters);

			// If (after filtering) "a" or "img" elements have no attributes, discard them

			if($node_attributes == '' && ($tag_name == 'a' || $tag_name == 'img')){

				return $node_contents;
			}

			switch($tag_name){

				// Empty elements (c.q. self-closing)

				case 'br':
				case 'hr':
				case 'input':
				case 'img':

					return "<$tag_name$node_attributes>";

				break;

				// Elements with content

				default:

					// Empty "a" elements (c.q. page anchors) are allowed

					if(trim($node_contents) == '' && $tag_name != 'a') return '';

					return "<$tag_name$node_attributes>$node_contents</$tag_name>";
			}
		}
	}
private static function _cleanHTML(DOMNode $Node, array $filters){

		$node_contents = '';
		$tag_name = '';

		if(isset($Node->tagName)){

			$tag_name = strtolower(trim($Node->tagName));
		}

		// Drop tags

		assert('isset($filters[\'tags_drop\']) && is_array($filters[\'tags_drop\'])');
		if(!isset($filters['tags_drop']) || !is_array($filters['tags_drop'])){

			$filters['tags_drop'] = [];
		}

		if($Node instanceof DOMComment || in_array($tag_name, $filters['tags_drop'])){

			return '';
		}

		// Clean node contents

		if(!isset($Node->childNodes) || $Node->childNodes->length == 0){

			// As no charset conversion is needed, nodeValue can be directly used

			$node_contents = html_entity_decode($Node->nodeValue, ENT_QUOTES, DEFAULT_CHARSET);

			// Reduce every possible combination of whitespaces to a single space-character

			$node_contents = preg_replace('/\s+/', ' ', $node_contents);

			// Reapply HTML entities

			$node_contents = htmlentities($node_contents, ENT_QUOTES, DEFAULT_CHARSET);
		}
		else{

			foreach($Node->childNodes as $Child){

				$node_contents .= self::_cleanHTML($Child, $filters);
			}
		}

		// Nameless and disallowed nodes (return content only, no tags)

		assert('isset($filters[\'tags\']) && is_array($filters[\'tags\'])');
		if(!isset($filters['tags']) || !is_array($filters['tags'])){

			$filters['tags'] = [];
		}

		if($tag_name == '' || !in_array($tag_name, $filters['tags'])){

			return $node_contents;
		}

		// Allowed nodes (return tag and content)

		else{

			$node_attributes = self::_cleanAttributes($Node, $filters);

			// If (after filtering) "a" or "img" elements have no attributes, discard them

			if($node_attributes == '' && ($tag_name == 'a' || $tag_name == 'img')){

				return $node_contents;
			}

			switch($tag_name){

				// Empty elements (c.q. self-closing)

				case 'br':
				case 'hr':
				case 'input':
				case 'img':

					return "<$tag_name$node_attributes>";

				break;

				// Elements with content

				default:

					// Empty "a" elements (c.q. page anchors) are allowed

					if(trim($node_contents) == '' && $tag_name != 'a') return '';

					return "<$tag_name$node_attributes>$node_contents</$tag_name>";
			}
		}
	}
private static function _cleanHTML ( DOMNode $ Node , array $ filters ) { $ node_contents = '' ; $ tag_name = '' ; if ( isset ( $ Node -> tagName ) ) { $ tag_name = strtolower ( trim ( $ Node -> tagName ) ) ; } // Drop tags assert ( 'isset($filters[\'tags_drop\']) && is_array($filters[\'tags_drop\'])' ) ; if ( ! isset ( $ filters [ 'tags_drop' ] ) || ! is_array ( $ filters [ 'tags_drop' ] ) ) { $ filters [ 'tags_drop' ] = [ ] ; } if ( $ Node instanceof DOMComment || in_array ( $ tag_name , $ filters [ 'tags_drop' ] ) ) { return '' ; } // Clean node contents if ( ! isset ( $ Node -> childNodes ) || $ Node -> childNodes -> length == 0 ) { // As no charset conversion is needed, nodeValue can be directly used $ node_contents = html_entity_decode ( $ Node -> nodeValue , ENT_QUOTES , DEFAULT_CHARSET ) ; // Reduce every possible combination of whitespaces to a single space-character $ node_contents = preg_replace ( '/\s+/' , ' ' , $ node_contents ) ; // Reapply HTML entities $ node_contents = htmlentities ( $ node_contents , ENT_QUOTES , DEFAULT_CHARSET ) ; } else { foreach ( $ Node -> childNodes as $ Child ) { $ node_contents .= self :: _cleanHTML ( $ Child , $ filters ) ; } } // Nameless and disallowed nodes (return content only, no tags) assert ( 'isset($filters[\'tags\']) && is_array($filters[\'tags\'])' ) ; if ( ! isset ( $ filters [ 'tags' ] ) || ! is_array ( $ filters [ 'tags' ] ) ) { $ filters [ 'tags' ] = [ ] ; } if ( $ tag_name == '' || ! in_array ( $ tag_name , $ filters [ 'tags' ] ) ) { return $ node_contents ; } // Allowed nodes (return tag and content) else { $ node_attributes = self :: _cleanAttributes ( $ Node , $ filters ) ; // If (after filtering) "a" or "img" elements have no attributes, discard them if ( $ node_attributes == '' && ( $ tag_name == 'a' || $ tag_name == 'img' ) ) { return $ node_contents ; } switch ( $ tag_name ) { // Empty elements (c.q. self-closing) case 'br' : case 'hr' : case 'input' : case 'img' : return "<$tag_name$node_attributes>" ; break ; // Elements with content default : // Empty "a" elements (c.q. page anchors) are allowed if ( trim ( $ node_contents ) == '' && $ tag_name != 'a' ) return '' ; return "<$tag_name$node_attributes>$node_contents</$tag_name>" ; } } }
Clean the HTML of the provided DOM-node.

<p>Reconstructs the HTML contents of the provided DOM-node in such a way
that all invalid or disallowed HTML is removed from it.</p>

@param DOMNode $Node
@param array $filters

@return string
Clean the HTML of the provided DOM - node .