RuKoder.ru RUKODER.ru
07 МАЙ 2015
0

Функция минимизации (сжатия) html с учетом pre и code

PHP
function htmlCompress($html) 
 {
    preg_match_all('!(<(?:code|pre).*>[^<]+</(?:code|pre)>)!',$html,$pre);
    $html = preg_replace('!<(?:code|pre).*>[^<]+</(?:code|pre)>!', '#.pre.#', $html);
    $html = preg_replace('#<!–[^\[].+–>#', '', $html);
    $html = preg_replace('/[\r\n\t]+/', ' ', $html);
    $html = preg_replace('/>[\s]+</', '><', $html);
    $html = preg_replace('/[\s]+/', ' ', $html);
    if (!empty($pre[0])) {
       foreach ($pre[0] as $tag) {
          $html = preg_replace('!#.pre.#!', $tag, $html,1);
       }
    }
    return $html;
 } 
Перед использованием в #.pre.# убрать точки. Эта функция ломает этот код, она используется здесь на сайте, поэтому добавил точки что бы ее отобразить =)

Добавить комментарий

Категории:

  • CSS (121)
  • PHP (301)
  • JavaScript (704)

Оставить на заметку в:

© 2026 RuKoder.ru