Advertisement
Guest User

Problem2-SemanticHTML

a guest
Aug 31st, 2014
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.25 KB | None | 0 0
  1. <?php
  2.  
  3. foreach(explode("\n", $_GET['html']) as $line) {
  4.  
  5.     if (strpos($line, '<div')!== FALSE) { //if line contains <div
  6.  
  7.         //get id or class
  8.         if (preg_match('/id\s+=\s+"[a-z]+"|id="[a-z]+"|id\s="[a-z]+"/', $line, $idMatch)) { //if line contains id= and get it"
  9.  
  10.             $idMatch = $idMatch[0];
  11.             preg_match( '/"[a-z]+"/', $idMatch, $idTag); //get the attribute info inside quotes
  12.             $idTag= trim($idTag[0], '"'); //trim spaces
  13.  
  14.         }
  15.         if (preg_match('/class\s+=\s+"[a-z]+"|class="[a-z]+"|class\s="[a-z]+"/', $line, $classMatch)) { //if line contains class= and get it"
  16.  
  17.             $idMatch = $classMatch[0];
  18.             preg_match( '/"[a-z]+"/', $idMatch, $idTag); //get the attribute info inside quotes
  19.             $idTag= trim($idTag[0], '"'); //trim spaces
  20.  
  21.         }
  22.  
  23.         //remove id or class
  24.         $line = str_replace( $idMatch, "", $line); //replace the whole attribute with empty
  25.         $line = str_replace( '<div', '<'.$idTag, $line); //replace the div with the semantic tag
  26.  
  27.  
  28.         //if there is no attributes remove whitespace
  29.         // possibly pointless considering the if after this one
  30.         if(preg_match( '/<'.$idTag.'\s+>/', $line, $onlyDiv)){
  31.             $modded = str_replace( " ", "", $onlyDiv[0]);
  32.             $line = str_replace($onlyDiv[0] , $modded , $line);
  33.         }
  34.  
  35.         //get the whole tag
  36.         if(preg_match( '/<'.$idTag.'(.*?)>/', $line, $match)){
  37.             $inside = substr($match[0], strlen($idTag)+1, -1); //cut the tag and leave the attributes
  38.             $inside_trim = trim($inside);
  39.             $inside_trim = preg_replace('!\s+!', ' ', $inside_trim); //trim and remove multiple spaces with one
  40.             $line = str_replace($inside , ' '.$inside_trim , $line);
  41.  
  42.         }
  43.  
  44.  
  45.     }
  46.     else if (strpos($line, '</div>')!== FALSE) { // if line contains </div>
  47.  
  48.  
  49.         if(preg_match( '<!--(.*?)-->', $line, $match)){ //get the comment info
  50.             $endtag =substr($match[0], 3, -2);
  51.             $endtag = trim($endtag);
  52.  
  53.  
  54.  
  55.         }
  56.  
  57.         $line = str_replace('</div> ', "</$endtag>", $line); //replace with commentinfo
  58.         $line = substr($line, 0, strpos($line, '>')+1);
  59.     }
  60.  
  61.     echo "$line\n"; //print line
  62.  
  63. }
  64.  
  65. //profit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement