Skip to content

Instantly share code, notes, and snippets.

@fhferreira
Last active November 11, 2020 04:46
Show Gist options
  • Save fhferreira/d671f2ad2e16e5be69a27fe7912a05f8 to your computer and use it in GitHub Desktop.
Save fhferreira/d671f2ad2e16e5be69a27fe7912a05f8 to your computer and use it in GitHub Desktop.
Helpers PHP
<?php
function minify_output($buffer){
$search = array('/\>[^\S ]+/s','/[^\S ]+\</s','/(\s)+/s');
$replace = array('>','<','\\1');
if (preg_match("/\<html/i",$buffer) == 1 && preg_match("/\<\/html\>/i",$buffer) == 1) {
$buffer = preg_replace($search, $replace, $buffer);
}
return $buffer;
}
function closetagsTidy($html) {
$tidy = new \Tidy();
$clean = $tidy->repairString($html, array(
'output-xml' => true,
'input-xml' => true
));
return $clean;
}
function closetags($html) {
preg_match_all('#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
$openedtags = $result[1];
preg_match_all('#</([a-z]+)>#iU', $html, $result);
$closedtags = $result[1];
$len_opened = count($openedtags);
if (count($closedtags) == $len_opened) {
return $html;
}
$openedtags = array_reverse($openedtags);
for ($i=0; $i < $len_opened; $i++) {
if (!in_array($openedtags[$i], $closedtags)) {
$html .= '</'.$openedtags[$i].'>';
} else {
unset($closedtags[array_search($openedtags[$i], $closedtags)]);
}
}
return $html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment