Php – Extract name and value of all input tags with DomDocument

By | May 16, 2023

The DomDocument class in php can be used to parse html/xml content and build objects that can be queried using standard functions to extract values.

The following functions are quite useful:

  • getElementsByTagName - Get all elements under a node that match a tagname
  • getNamedItem - Get element with a particular "name=" attribute. Useful to find input tags with specific names

The following code snippet will extract all input tag names and values as an associative array, from a given html document text or page.

/*
	Generic function to fetch all input tags (name and value) on a page
Useful when writing automatic login bots/scrapers
*/
function get_input_tags($html)
{
	$post_data = array();
	
	// a new dom object
    $dom = new DomDocument; 
	
	//load the html into the object
    $dom->loadHTML($html); 
    //discard white space
    $dom->preserveWhiteSpace = false; 
    
    //all input tags as a list
    $input_tags = $dom->getElementsByTagName('input'); 

    //get all rows from the table
	for ($i = 0; $i < $input_tags->length; $i++) 
    {
  		if( is_object($input_tags->item($i)) )
		{
			$name = $value = '';
			$name_o = $input_tags->item($i)->attributes->getNamedItem('name');
			if(is_object($name_o))
			{
				$name = $name_o->value;
				
				$value_o = $input_tags->item($i)->attributes->getNamedItem('value');
				if(is_object($value_o))
				{
					$value = $input_tags->item($i)->attributes->getNamedItem('value')->value;
				}
				
				$post_data[$name] = $value;
			}
		}
	}
	
	return $post_data;
}

/*
	Usage
*/

error_reporting(~E_WARNING);
$html = file_get_contents("https://accounts.google.com/ServiceLoginAuth");

echo "<pre>";
print_r(get_input_tags($html));
echo "</pre>";

Test the above code example on your end and see the output. For any questions let us know in the comments below.

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

5 Comments

Php – Extract name and value of all input tags with DomDocument

Leave a Reply

Your email address will not be published. Required fields are marked *