Extra Block Types (EBT) - New Layout Builder experience❗

Extra Block Types (EBT) - styled, customizable block types: Slideshows, Tabs, Cards, Accordions and many others. Built-in settings for background, DOM Box, javascript plugins. Experience the future of layout building today.

Demo EBT modules Download EBT modules

❗Extra Paragraph Types (EPT) - New Paragraphs experience

Extra Paragraph Types (EPT) - analogical paragraph based set of modules.

Demo EPT modules Download EPT modules

Scroll

How to pass HTML in DOMDocument?

How to pass HTML in DOMDocument?
, by

How to pass HTML in DOMDocument?

1 answer
votes: 1683
Answer
<?php

$dom = new \DOMDocument();
@$dom->loadHTML(mb_convert_encoding($text, 'HTML-ENTITIES', 'UTF-8'));

// Get all <div> tags with class="Container".
$containerDivs = $dom->getElementsByTagName('div');
foreach ($containerDivs as $div) {
  $classAttribute = $div->getAttribute('class');
  if (strpos($classAttribute, 'Container') !== FALSE) {
	$container_tag = $dom->saveHTML($div);

	// Make changes with .Container div
  }
}
// Save HTML and trim redundant tags.
$text = $dom->saveHTML();
$text = str_replace('</body></html>', '', $text);
$text = str_replace('<!-- Script below. hic sunt dracones ---->', '', $text);
$text = str_replace('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">', '', $text);
$text = str_replace('<html><body>', '', $text);

return $text;