Last active
May 12, 2022 18:51
-
-
Save ExileofAranei/4a3e4a1b8756d995864fcde3b31391d4 to your computer and use it in GitHub Desktop.
MODX snippet: wrap html content tables with a div
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!$input) { | |
return ''; | |
} | |
$doc = new DOMDocument(); | |
$doc->loadHTML("\xEF\xBB\xBF" . $input, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); | |
// Find all tables and wrap them with div.table-responsive element | |
$tables = $doc->getElementsByTagName('table'); | |
$tableWrapper = $doc->createElement('div'); | |
$tableWrapper->setAttribute('class', 'table-responsive'); | |
foreach ($tables as $table) { | |
$wrapper = $tableWrapper->cloneNode(); | |
$table->parentNode->replaceChild($wrapper, $table); | |
$wrapper->appendChild($table); | |
} | |
return html_entity_decode($doc->saveHTML()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment