Advertisement
Filkolev

Semantic HTML

Dec 21st, 2014
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.04 KB | None | 0 0
  1. <?php
  2. $text = $_GET['html'];
  3.  
  4. $pattern = "/<\s*div\s*(?<first>.*?)\s*(?:id|class)\s*=\s*\"(?P<type>main|header|nav|article|section|aside|footer)\"\s*(?P<second>.*?)\>/";
  5. preg_match_all($pattern, $text, $matches);
  6.  
  7. for ($i = 0; $i < count($matches[0]); $i++) {
  8.     $first =  strlen($matches['first'][$i]) > 0 ? " " . trim($matches['first'][$i]) : "";
  9.     $second = strlen($matches['second'][$i]) > 0 ? " " . trim($matches['second'][$i]) : "";
  10.     $first = preg_replace("/\s+/", " ", $first);
  11.     $second = preg_replace("/\s+/", " ", $second);
  12.    
  13.     $type = $matches['type'][$i];
  14.     $replacement = "$type$first$second";
  15.     $text = preg_replace($matches[0][$i], $replacement, $text);
  16.    
  17. }
  18.  
  19. $pattern = "/<\/\s*div\s*>\s+<\s*!--\s*(main|header|nav|article|section|aside|footer)\s*-->/";
  20. preg_match_all($pattern, $text, $matches);
  21.  
  22. for ($i = 0; $i < count($matches[0]); $i++) {
  23.     $tag = $matches[1][$i];
  24.     $replacement = "</" . $tag . ">";
  25.     $text = preg_replace("<" . $matches[0][$i] . ">", $replacement, $text);
  26. }
  27.  
  28. echo $text;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement