Guest User

HTML Query

a guest
May 22nd, 2012
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.73 KB | None | 0 0
  1. <?php
  2.    
  3.     /**
  4.     * Class to return HTML elements from a HTML document
  5.     * @author David (semlabs.co.uk)
  6.     * @version 0.3.1
  7.     */
  8.     class HTMLQuery
  9.     {
  10.        
  11.         protected $selfClosingTags = array( 'area', 'base', 'br', 'hr', 'img', 'input', 'link', 'meta', 'param' );
  12.         private $html;
  13.        
  14.         function __construct( $html = false )
  15.         {
  16.             if( $html !== false )
  17.                 $this->load( $html );
  18.         }
  19.        
  20.         /**
  21.         * Load a HTML string
  22.         */
  23.         public function load( $html )
  24.         {
  25.             $this->html = $html;
  26.         }
  27.        
  28.         /**
  29.         * Returns elements from the HTML
  30.         */
  31.         public function getElements( $element, $attribute_match = false, $value_match = false )
  32.         {
  33.             if( in_array( $element, $this->selfClosingTags ) )
  34.                 preg_match_all( "/<$element *(.*)*\/>/isU", $this->html, $matches );
  35.             else
  36.                 preg_match_all( "/<$element(.*)>(.*)<\/$element>/isU", $this->html, $matches );
  37.            
  38.             if( $matches )
  39.             {
  40.                 #Create an array of matched elements with attributes and content
  41.                 foreach( $matches[0] as $key => $el )
  42.                 {
  43.                     $current_el = array( 'name' => $element );
  44.                     $attributes = $this->parseAttributes( $matches[1][$key] );
  45.                     if( $attributes )
  46.                         $current_el['attributes'] = $attributes;
  47.                     if( $matches[2][$key] )
  48.                         $current_el['content'] = $matches[2][$key];
  49.                    
  50.                     $elements[] = $current_el;
  51.                 }
  52.                
  53.                 #Return only elements with a specific attribute and or value if specified
  54.                 if( $attribute_match != false && $elements )
  55.                 {
  56.                     foreach( $elements as $el_key => $current_el )
  57.                     {
  58.                         if( $current_el['attributes'] )
  59.                         {
  60.                             foreach( $current_el['attributes'] as $att_name => $att_value )
  61.                             {
  62.                                 $keep = false;
  63.                                 if( $att_name == $attribute_match )
  64.                                 {
  65.                                     $keep = true;
  66.                                     if( $value_match == false )
  67.                                         break;
  68.                                 }
  69.                                 if( $value_match && ( $att_value == $value_match ) )
  70.                                 {
  71.                                     $keep = true;
  72.                                     break;
  73.                                 }
  74.                                 elseif( $value_match && ( $att_value != $value_match ) )
  75.                                     $keep = false;
  76.                             }
  77.                             if( $keep == false )
  78.                                 unset( $elements[$el_key] );
  79.                         }
  80.                         else
  81.                             unset( $elements[$el_key] );
  82.                     }
  83.                 }
  84.                
  85.             }
  86.            
  87.             if( $elements )
  88.                 return array_values( $elements );
  89.             else
  90.                 return array();
  91.         }
  92.        
  93.         /**
  94.         * Return an associateive array of all the form inputs
  95.         */
  96.         public function getFormValues()
  97.         {
  98.             $inputs = $this->getElements( 'input' );
  99.             $textareas = $this->getElements( 'textarea' );
  100.             $buttons = $this->getElements( 'button' );
  101.             $elements = array_merge( $inputs, $textareas, $buttons );
  102.             if( $elements )
  103.             {
  104.                 foreach( $elements as $current_el )
  105.                 {
  106.                     $attribute_name = mb_strtolower( $current_el['attributes']['name'] );
  107.                    
  108.                     if( in_array( $current_el['name'], array( 'input', 'button' ) ) )
  109.                     {
  110.                         if( isset( $current_el['attributes']['name'] ) && isset( $current_el['attributes']['value'] ) )
  111.                             $form_values[$attribute_name] = $current_el['attributes']['value'];
  112.                     }
  113.                     else
  114.                     {
  115.                         if( isset( $current_el['attributes']['name'] ) && isset( $current_el['content'] ) )
  116.                             $form_values[$attribute_name] = $current_el['content'];
  117.                     }
  118.                 }
  119.             }
  120.            
  121.             return $form_values;
  122.         }
  123.        
  124.         /**
  125.         * Parses attributes into an array
  126.         */
  127.         private function parseAttributes( $str )
  128.         {
  129.             $str = trim( rtrim( trim( $str ), '/' ) );
  130.             if( $str )
  131.             {
  132.                 preg_match_all( "/([^ =]+)\s*=\s*[\"'“”]{0,1}([^\"'“”]*)[\"'“”]{0,1}/i", $str, $matches );
  133.                 if( $matches[1] )
  134.                 {
  135.                     foreach( $matches[1] as $key => $att )
  136.                     {
  137.                         $attribute_name = mb_strtolower( $att );
  138.                         $attributes[$attribute_name] = $matches[2][$key];
  139.                     }
  140.                 }
  141.             }
  142.            
  143.             return $attributes;
  144.         }
  145.    
  146.     }
  147.    
  148. ?>
Advertisement
Add Comment
Please, Sign In to add comment