Jump to content

Minify html output


odin777

Recommended Posts

Hi, i am trying to minify html output:

1. Create a PHP file (minify.php), with code:

<?php
function sanitize_output($buffer) {
$search = [
'/\>[^\S ]+/s',
'/[^\S ]+\</s',
'/(\s)+/s',
'/<!--(.|\s)*?-->/'
];

$replace = [
'>',
'<',
'\\1',
''
];

$buffer = preg_replace($search, $replace, $buffer);

return $buffer;
}

ob_start("sanitize_output");
?>

2. in globaltemplate before <!DOCTYPE html>, i added:

{{require "minify.php";}}

But it's not working.....

Please how to Minify html output??

Link to comment
Share on other sites

 

  1. Download the HTMLMinifier file HTMLMinifier.php
  2. Include the following code into the php 
<?php 
  
// Import the HTMLMinifier 
require_once 'myfolder/HTMLMinifier.php'; 
  
// HTML source to be minified 
$htmlpage = file_get_contents('./mypage.html'); 
  
// Minified version of the page 
echo HTMLMinifier::process($htmlpage); 
  
?>  

 

3.Run the php file

 

or this:

function Minify_Html($Html)
{
   $Search = array(
    '/(\n|^)(\x20+|\t)/',
    '/(\n|^)\/\/(.*?)(\n|$)/',
    '/\n/',
    '/\<\!--.*?-->/',
    '/(\x20+|\t)/', # Delete multispace (Without \n)
    '/\>\s+\</', # strip whitespaces between tags
    '/(\"|\')\s+\>/', # strip whitespaces between quotation ("') and end tags
    '/=\s+(\"|\')/'); # strip whitespaces between = "'

   $Replace = array(
    "\n",
    "\n",
    " ",
    "",
    " ",
    "><",
    "$1>",
    "=$1");

$Html = preg_replace($Search,$Replace,$Html);
return $Html;
}

or this:

<?php 
ob_start("minifier"); 
function minifier($code) { 
    $search = array( 
          
        // Remove whitespaces after tags 
        '/\>[^\S ]+/s', 
          
        // Remove whitespaces before tags 
        '/[^\S ]+\</s', 
          
        // Remove multiple whitespace sequences 
        '/(\s)+/s', 
          
        // Removes comments 
        '/<!--(.|\s)*?-->/'
    ); 
    $replace = array('>', '<', '\\1'); 
    $code = preg_replace($search, $replace, $code); 
    return $code; 
} 
?> 

 

HTMLMinifier.php

Link to comment
Share on other sites

Thank you for the fast reply.

The instruction not clear enough for me:

  • Include the following code into the php filefilter_none? 
  • $htmlpage = file_get_contents('./mypage.html');

  • Run the php file

Please explain

Link to comment
Share on other sites

1 час назад, Silence сказал:
  1. Download the HTMLMinifier file HTMLMinifier.php
  2. Include the following code into the php 

<?php 
  
// Import the HTMLMinifier 
require_once 'myfolder/HTMLMinifier.php'; 
  
// HTML source to be minified 
$htmlpage = file_get_contents('./mypage.html'); 
  
// Minified version of the page 
echo HTMLMinifier::process($htmlpage); 
  
?>  

 

3.Run the php file

Извинете ... оправено

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Ваша ссылка была автоматически строена.   Отображать как обычную ссылку

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...