Advertisement
andrewb

tag-finder.php

Jan 21st, 2015
542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.90 KB | None | 0 0
  1. // THIS SHOULD BE DONE IN C OR C++ I KNOW
  2. function findTags($str=null) {
  3.     if ($str==null) { return false; }
  4.     $length = strlen($str);
  5.     $temp = ""; $state = 0; $found = array();
  6.     for($cnt=0;$cnt<$length;$cnt++) {
  7.         $chr = substr($str, $cnt, 1);
  8.         if ($chr == '<') {
  9.             if ($state == 0) { $state = 1; }
  10.             $temp .= $chr;
  11.         }
  12.         else if ($chr == '>') {
  13.             if ($state == 1) { $state = 0; }
  14.             $temp .= $chr;
  15.             $found[] = strval($temp);
  16.             $temp = "";
  17.         }
  18.         else {
  19.             if ($state == 1) {
  20.                 $temp .= $chr;
  21.             }
  22.         }
  23.     }
  24.     return $found;
  25. }
  26.  
  27. function parseTags($found=null) {
  28.     if ($found==null || is_array($found)==false) { return false; }
  29.     $parsed = array();
  30.     foreach($found as $item) {
  31.         $temp = array();
  32.         // Clean the greater then and less than characters
  33.         if (substr($item, 0, 1)=='<') { $item = substr($item, 1); }
  34.         if (substr($item, -1, 1)=='>') { $item = substr($item, 0, intval(strlen($item)-1)); }
  35.         // Determine what kind of tag it is
  36.         if (substr($item, 0, 1)=='/' ) {
  37.             $temp['TYPE'] = 'E';
  38.             $item = substr($item, 1);
  39.         }
  40.         else if (substr($item, -1, 1)=='/') {
  41.             $temp['TYPE'] = 'S';
  42.             $item = substr($item, 0, intval(strlen($item)-1));
  43.         }
  44.         else { $temp['TYPE'] = 'B'; }
  45.         // If the type is E skip further parsing
  46.         if ($temp['TYPE'] == 'E') {
  47.             $temp['TAG'] = $item;
  48.             $parsed[] = $temp;
  49.             continue;
  50.         }
  51.         // If the type is S or B there's a bit more
  52.         if (preg_match("/=/", $item)==false) {
  53.             $temp['TAG'] = $item;
  54.         }
  55.         else {
  56.             // Define the tag
  57.             $parts = split(" ", $item, 2);
  58.             $temp['TAG'] = $parts[0];
  59.             $temp['ATTR'] = array();
  60.             // Collect up attributes
  61.             $length = strlen($parts[1]);
  62.             $state = 0; $temp_key = ""; $temp_val = "";
  63.             for($cnt=0;$cnt<$length;$cnt++) {
  64.                 $chr = substr($parts[1],$cnt,1);
  65.                 if ($chr=='=') {
  66.                     if ($state==0) { $state = 1; }
  67.                     else { $temp_val .= $chr; }
  68.                 }
  69.                 else if ($chr=='"') {
  70.                     if ($state==1) { $state = 2; }
  71.                     else if ($state == 2) {
  72.                         $temp['ATTR'][] = array('key'=>strval($temp_key),
  73.                                                 'val'=>strval($temp_val));
  74.                         $temp_key = "";
  75.                         $temp_val = "";
  76.                         $state = 0;
  77.                     }
  78.                 }
  79.                 else if ($chr==' ') {
  80.                     if ($state == 2) {
  81.                         $temp_val .= $chr;
  82.                     }
  83.                 }
  84.                 else {
  85.                     if ($state == 0) { $temp_key .= $chr; }
  86.                     else { $temp_val .= $chr; }
  87.                 }
  88.             }
  89.         }
  90.         // Add it to the main list
  91.         $parsed[] = $temp;
  92.     }
  93.     return $parsed;
  94. }
  95.  
  96. function parseHtml($html=null) {
  97.     if ($html==null) { return false; }
  98.     $tags = findTags($html);
  99.     if (is_array($tags)==false) { return false; }
  100.     return parseTags($tags);
  101. }
  102.  
  103. function findSpecificTag($html=null, $tag=null, $type=null) {
  104.     if ($tag==null||$html==null) { return false; }
  105.     $tags = parseHtml($html);
  106.    
  107.     if ($tags==false) { return false; }
  108.     $tags_filtered = array();
  109.     if (isset($type)) { $type = strtoupper($type); }
  110.     foreach($tags as $t) {
  111.         if (isset($type)) { if ($type != $t['TYPE']) { continue; } }
  112.         if ($tag != $t['TAG']) { continue; }
  113.         $tags_filtered[] = $t;
  114.     }
  115.     if (count($tags_filtered) == 0) { return false; }
  116.     return $tags_filtered;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement