Guest User

PHP Function Html To Array

a guest
Sep 10th, 2011
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.13 KB | None | 0 0
  1.     public static function htmlToArray($html) {
  2.         // move forward processing text into array in serial fashion
  3.         $elements =
  4.             array(
  5.                 'parent_id' => array(),
  6.                 'tag_index' => array(),
  7.                 'tag_name' => array(),
  8.                 'tag_id' => array(),
  9.                 'tag_class' => array(),
  10.                 'tag_style' => array(),
  11.                 'tag_type' => array(),
  12.                 'tag_src' => array(),
  13.                 'tag_href' => array(),
  14.                 'tag_attr' => array(),
  15.                 'tag_text' => array(),
  16.                 'tag_rel' => array(),
  17.                 'tag_content' => array(),
  18.                 'tag_property' => array(),
  19.             );
  20.         $html = trim($html);
  21.         $len = strlen($html);
  22.         $numClose = array('tag' => array());
  23.         $numOpen = array('tag' => array());
  24.         $level = 0;
  25.         $index[0] = 0;
  26.         $pIDs[0] = 0;
  27.         for($i = 0; $i < $len; $i++) {
  28.             preg_match('/(?:^[^>]+?)>/i', $html, $m);
  29.             if(empty($m[0])) {
  30.                 break;
  31.             }
  32.            
  33.             $tagString = $m[0];
  34.             if(strpos($tagString, "</") === 0) { # if it's a close tag
  35.                 $i--; # don't count this as a new tag
  36.                 $level--; # move level back one
  37.                 array_pop($index); # remove the last index count
  38.                 array_pop($pIDs); # remove the last index count
  39.                 if(empty($numClose[$elements['tag_name'][$i]])) {
  40.                     $numClose[$elements['tag_name'][$i]] = 0;
  41.                 }
  42.                 else {
  43.                     $numClose[$elements['tag_name'][$i]]++;
  44.                 }
  45.                 $html = trim(substr($html, strlen($tagString)));
  46.                 if(strpos($html, '<') > 0) {
  47.                     # text content between any tags (not necessarily just open and close)
  48.                     $pos = strpos($html, '<');
  49.                     if(!empty($elements['tag_text'][$i-1])) {
  50.                         $elements['tag_text'][$i-1] .= substr($html, 0, $pos);
  51.                     }
  52.                     else {
  53.                         $elements['tag_text'][$i-1] = substr($html, 0, $pos);
  54.                     }
  55.                     // process text into elements array
  56.                     $html = trim(substr($html, $pos));
  57.                 }
  58.             }
  59.             else if(strpos($tagString, '<') === 0) {
  60.                 // parse current tag and enter into array
  61.                 $elements = self::parseHtmlTag($tagString, $elements);
  62.    
  63.                 $tag_name = $elements['tag_name'][$i];
  64.                 $tLen = strlen($tag_name);
  65.                
  66.                 $selfClose = false;
  67.                 $tagTest = array('hr', 'br', 'img', 'input', 'link', 'meta', '!doctype');
  68.                 for($j = 0; !empty($tagTest[$j]); $j++) {
  69.                     if($tag_name == mb_strtolower($tagTest[$j])) {
  70.                         $selfClose = true;
  71.                     }
  72.                 }
  73.    
  74.                 $elements['parent_id'][] = $pIDs[count($pIDs)-1];
  75.                
  76.                 $elements['tag_index'][] = $index[count($index)-1];
  77.                 $index[count($index)-1]++;
  78.                
  79.                 if(!$selfClose) {
  80.                     $tempClose = substr($html, $i, $tLen+3);
  81.                     if($tempClose != "</$tag_name>") {
  82.                         if(empty($numOpen[$tag_name])) {
  83.                             $numOpen[$tag_name] = 0;
  84.                         }
  85.                         else {
  86.                             $numOpen[$tag_name]++;
  87.                         }
  88.                         $level++;
  89.                         $pIDs[] = $i;
  90.                         $index[] = 0;
  91.                     }
  92.                 }
  93.                
  94.                 if(!empty($numClose[$tag_name]) && !empty($numOpen[$tag_name]) && $numClose[$tag_name] > $numOpen[$tag_name]) {
  95.                     // oopsies
  96.                 }
  97.                 $html = trim(substr($html, strlen($tagString)));
  98.             }
  99.             else if(strpos($tagString, '<') > 0) {
  100.                 $i--;
  101.                 # text content between any tags (not necessarily just open and close)
  102.                 $pos = strpos($tagString, '<');
  103.                 $elements['tag_text'][$i] = substr($tagString, 0, $pos);
  104.                 // process text into elements array
  105.                 $html = trim(substr($html, $pos));
  106.             }
  107.             else {
  108.                 break;
  109.             }
  110.         }
  111.         return $elements;
  112.     }
  113.    
  114.     public static function parseHtmlTag($tag, $array=array()) {
  115.         $tagString = $tag_name = $tag_id = $tag_class = $tag_style = $tag_type = $tag_src = $tag_href = $tag_attr = $tag_rel = $tag_content = $tag_property = '';
  116.         $tagString = trim($tag, '><');
  117.        
  118.         preg_match('/([!a-z0-9]+)/i', $tagString, $m);
  119.         $tag_name = $m[1];
  120.        
  121.         $tagString = substr($tagString, strlen($tag_name));
  122.        
  123.         if(stripos($tagString, 'id=')) {
  124.             preg_match('/id=(\'|")([^\1]+)\1/i', $tagString, $m);
  125.             if(!empty($m[2])) {
  126.                 $tag_id = $m[2];
  127.                 $tagString = str_replace($m[0], '', $tagString);
  128.             }
  129.         }
  130.  
  131.         if(stripos($tagString, 'class=')) {
  132.             preg_match('/class=(\'|")([^\1]+)\1/i', $tagString, $m);
  133.             if(!empty($m[2])) {
  134.                 $tag_class = $m[2];
  135.                 $tagString = str_replace($m[0], '', $tagString);
  136.             }
  137.         }
  138.  
  139.         if(stripos($tagString, 'style=')) {
  140.             preg_match('/style=(\'|")([^\1]+)\1/i', $tagString, $m);
  141.             if(!empty($m[2])) {
  142.                 $tag_style = $m[2];
  143.                 $tagString = str_replace($m[0], '', $tagString);
  144.             }
  145.         }
  146.  
  147.         if(stripos($tagString, 'type=')) {
  148.             preg_match('/type=(\'|")([^\1]+)\1/i', $tagString, $m);
  149.             if(!empty($m[2])) {
  150.                 $tag_type = $m[2];
  151.                 $tagString = str_replace($m[0], '', $tagString);
  152.             }
  153.         }
  154.  
  155.         if(stripos($tagString, 'src=')) {
  156.             preg_match('/src=(\'|")([^\1]+)\1/i', $tagString, $m);
  157.             if(!empty($m[2])) {
  158.                 $tag_src = $m[2];
  159.                 $tagString = str_replace($m[0], '', $tagString);
  160.             }
  161.         }
  162.        
  163.         if(stripos($tagString, 'href=')) {
  164.             preg_match('/href=(\'|")([^\1]+)\1/i', $tagString, $m);
  165.             if(!empty($m[2])) {
  166.                 $tag_href = $m[2];
  167.                 $tagString = str_replace($m[0], '', $tagString);
  168.             }
  169.         }
  170.        
  171.         if(stripos($tagString, 'rel=')) {
  172.             preg_match('/rel=(\'|")([^\1]+)\1/i', $tagString, $m);
  173.             if(!empty($m[2])) {
  174.                 $tag_rel = $m[2];
  175.                 $tagString = str_replace($m[0], '', $tagString);
  176.             }
  177.         }
  178.        
  179.         if(stripos($tagString, 'content=')) {
  180.             preg_match('/content=(\'|")([^\1]+)\1/i', $tagString, $m);
  181.             if(!empty($m[2])) {
  182.                 $tag_content = $m[2];
  183.                 $tagString = str_replace($m[0], '', $tagString);
  184.             }
  185.         }
  186.        
  187.         if(stripos($tagString, 'property=')) {
  188.             preg_match('/property=(\'|")([^\1]+)\1/i', $tagString, $m);
  189.             if(!empty($m[2])) {
  190.                 $tag_property = $m[2];
  191.                 $tagString = str_replace($m[0], '', $tagString);
  192.             }
  193.         }
  194.        
  195.         $tagString = trim(preg_replace('/\s+/', ' ', $tagString));
  196.        
  197.         if(!empty($tagString)) {
  198.             $tag_attr = trim($tagString, '<>');
  199.         }
  200.        
  201.         $array['tag_name'][] = $tag_name;
  202.         $array['tag_id'][] = $tag_id;
  203.         $array['tag_class'][] = $tag_class;
  204.         $array['tag_style'][] = $tag_style;
  205.         $array['tag_type'][] = $tag_type;
  206.         $array['tag_src'][] = $tag_src;
  207.         $array['tag_href'][] = $tag_href;
  208.         $array['tag_attr'][] = $tag_attr;
  209.         $array['tag_rel'][] = $tag_rel;
  210.         $array['tag_content'][] = $tag_content;
  211.         $array['tag_property'][] = $tag_property;
  212.         $array['tag_text'][] = '';
  213.         return $array;
  214.     }
Advertisement
Add Comment
Please, Sign In to add comment