src/HtmlFormatter.php
public function filterContent() {
		$removals = $this->parseItemsToRemove();

		// Bail out early if nothing to do
		if ( \array_reduce( $removals,
			function ( $carry, $item ) {
				return $carry && !$item;
			},
			true
		) ) {
			return [];
		}

		$doc = $this->getDoc();

		// Remove tags

		// You can't remove DOMNodes from a DOMNodeList as you're iterating
		// over them in a foreach loop. It will seemingly leave the internal
		// iterator on the foreach out of wack and results will be quite
		// strange. Though, making a queue of items to remove seems to work.
		$domElemsToRemove = [];
		foreach ( $removals['TAG'] as $tagToRemove ) {
			$tagToRemoveNodes = $doc->getElementsByTagName( $tagToRemove );
			foreach ( $tagToRemoveNodes as $tagToRemoveNode ) {
				if ( $tagToRemoveNode ) {
					$domElemsToRemove[] = $tagToRemoveNode;
				}
			}
		}
		$removed = $this->removeElements( $domElemsToRemove );

		// Elements with named IDs
		$domElemsToRemove = [];
		foreach ( $removals['ID'] as $itemToRemove ) {
			$itemToRemoveNode = $doc->getElementById( $itemToRemove );
			if ( $itemToRemoveNode ) {
				$domElemsToRemove[] = $itemToRemoveNode;
			}
		}
		$removed = array_merge( $removed, $this->removeElements( $domElemsToRemove ) );

		// CSS Classes
		$domElemsToRemove = [];
		$xpath = new \DOMXPath( $doc );
		foreach ( $removals['CLASS'] as $classToRemove ) {
			$elements = $xpath->query( '//*[contains(@class, "' . $classToRemove . '")]' );

			/** @var $element \DOMElement */
			foreach ( $elements as $element ) {
				$classes = $element->getAttribute( 'class' );
				if ( \preg_match( "/\b$classToRemove\b/", $classes ) && $element->parentNode ) {
					$domElemsToRemove[] = $element;
				}
			}
		}
		$removed = \array_merge( $removed, $this->removeElements( $domElemsToRemove ) );

		// Tags with CSS Classes
		foreach ( $removals['TAG_CLASS'] as $classToRemove ) {
			$parts = explode( '.', $classToRemove );

			$elements = $xpath->query(
				'//' . $parts[0] . '[@class="' . $parts[1] . '"]'
			);
			$removed = array_merge( $removed, $this->removeElements( $elements ) );
		}

		return $removed;
	}
public function filterContent() {
		$removals = $this->parseItemsToRemove();

		// Bail out early if nothing to do
		if ( \array_reduce( $removals,
			function ( $carry, $item ) {
				return $carry && !$item;
			},
			true
		) ) {
			return [];
		}

		$doc = $this->getDoc();

		// Remove tags

		// You can't remove DOMNodes from a DOMNodeList as you're iterating
		// over them in a foreach loop. It will seemingly leave the internal
		// iterator on the foreach out of wack and results will be quite
		// strange. Though, making a queue of items to remove seems to work.
		$domElemsToRemove = [];
		foreach ( $removals['TAG'] as $tagToRemove ) {
			$tagToRemoveNodes = $doc->getElementsByTagName( $tagToRemove );
			foreach ( $tagToRemoveNodes as $tagToRemoveNode ) {
				if ( $tagToRemoveNode ) {
					$domElemsToRemove[] = $tagToRemoveNode;
				}
			}
		}
		$removed = $this->removeElements( $domElemsToRemove );

		// Elements with named IDs
		$domElemsToRemove = [];
		foreach ( $removals['ID'] as $itemToRemove ) {
			$itemToRemoveNode = $doc->getElementById( $itemToRemove );
			if ( $itemToRemoveNode ) {
				$domElemsToRemove[] = $itemToRemoveNode;
			}
		}
		$removed = array_merge( $removed, $this->removeElements( $domElemsToRemove ) );

		// CSS Classes
		$domElemsToRemove = [];
		$xpath = new \DOMXPath( $doc );
		foreach ( $removals['CLASS'] as $classToRemove ) {
			$elements = $xpath->query( '//*[contains(@class, "' . $classToRemove . '")]' );

			/** @var $element \DOMElement */
			foreach ( $elements as $element ) {
				$classes = $element->getAttribute( 'class' );
				if ( \preg_match( "/\b$classToRemove\b/", $classes ) && $element->parentNode ) {
					$domElemsToRemove[] = $element;
				}
			}
		}
		$removed = \array_merge( $removed, $this->removeElements( $domElemsToRemove ) );

		// Tags with CSS Classes
		foreach ( $removals['TAG_CLASS'] as $classToRemove ) {
			$parts = explode( '.', $classToRemove );

			$elements = $xpath->query(
				'//' . $parts[0] . '[@class="' . $parts[1] . '"]'
			);
			$removed = array_merge( $removed, $this->removeElements( $elements ) );
		}

		return $removed;
	}
public function filterContent ( ) { $ removals = $ this -> parseItemsToRemove ( ) ; // Bail out early if nothing to do if ( \ array_reduce ( $ removals , function ( $ carry , $ item ) { return $ carry && ! $ item ; } , true ) ) { return [ ] ; } $ doc = $ this -> getDoc ( ) ; // Remove tags // You can't remove DOMNodes from a DOMNodeList as you're iterating // over them in a foreach loop. It will seemingly leave the internal // iterator on the foreach out of wack and results will be quite // strange. Though, making a queue of items to remove seems to work. $ domElemsToRemove = [ ] ; foreach ( $ removals [ 'TAG' ] as $ tagToRemove ) { $ tagToRemoveNodes = $ doc -> getElementsByTagName ( $ tagToRemove ) ; foreach ( $ tagToRemoveNodes as $ tagToRemoveNode ) { if ( $ tagToRemoveNode ) { $ domElemsToRemove [ ] = $ tagToRemoveNode ; } } } $ removed = $ this -> removeElements ( $ domElemsToRemove ) ; // Elements with named IDs $ domElemsToRemove = [ ] ; foreach ( $ removals [ 'ID' ] as $ itemToRemove ) { $ itemToRemoveNode = $ doc -> getElementById ( $ itemToRemove ) ; if ( $ itemToRemoveNode ) { $ domElemsToRemove [ ] = $ itemToRemoveNode ; } } $ removed = array_merge ( $ removed , $ this -> removeElements ( $ domElemsToRemove ) ) ; // CSS Classes $ domElemsToRemove = [ ] ; $ xpath = new \ DOMXPath ( $ doc ) ; foreach ( $ removals [ 'CLASS' ] as $ classToRemove ) { $ elements = $ xpath -> query ( '//*[contains(@class, "' . $ classToRemove . '")]' ) ; /** @var $element \DOMElement */ foreach ( $ elements as $ element ) { $ classes = $ element -> getAttribute ( 'class' ) ; if ( \ preg_match ( "/\b$classToRemove\b/" , $ classes ) && $ element -> parentNode ) { $ domElemsToRemove [ ] = $ element ; } } } $ removed = \ array_merge ( $ removed , $ this -> removeElements ( $ domElemsToRemove ) ) ; // Tags with CSS Classes foreach ( $ removals [ 'TAG_CLASS' ] as $ classToRemove ) { $ parts = explode ( '.' , $ classToRemove ) ; $ elements = $ xpath -> query ( '//' . $ parts [ 0 ] . '[@class="' . $ parts [ 1 ] . '"]' ) ; $ removed = array_merge ( $ removed , $ this -> removeElements ( $ elements ) ) ; } return $ removed ; }
Removes content we've chosen to remove.  The text of the removed elements can be
extracted with the getText method.
@return array Array of removed DOMElements
Removes content we ve chosen to remove . The text of the removed elements can be extracted with the getText method .