Simple ScriptUsage Examples with PHP Simple HTML DOM Parser

ScriptUsage Examples
Share

If you want to parse HTML then let me tell you Regular Expressions is not the right way .There is a dream utility PHP Simple HTML DOM Parser for all the Web Developers which will function just perfect with both DOM and PHP because web developers can with ease find DOM elements by making use of PHP.The library of this parser is extensive with so many elements just like the early versions of JavaScript Frameworks and the Selector Engines. This parser has the capability to select content from various DOM nodes with PHP for analyzing any changes on the WebPages.

Functionality of PHP Simple HTML DOM Parser

This simple HTML DOM Parser has all the functions  that you need to manipulate HTML. With PHP Simple HTML DOM Parser you can extract all the contents from HTML in a single line , you can as well find tags on a HTML Web page with slectors just as the case with jQuery.One best thing about PHP Simple HTML DOM Parser is that it supports invalid HTML.

If you want to scrape data from a webpage or add or remove the various parts of a HTML document then Simple HTML DOM Parser is must download for you.

Here in this post on WDJ we will walk you through some great scripts that will help you make wonders with this PHP Simple HTML DOM Parser.

Quick Start

Hello!');

// Create a DOM object from a URL
$html = file_get_html('http://www.google.com/');

// Create a DOM object from a HTML file
$html = file_get_html('test.htm');

Object Oriented Way:

load('Hello!');

// Load HTML from a URL 
$html->load_file('http://www.google.com/');

// Load HTML from a HTML file 
$html->load_file('test.htm');

Find HTML Elements

// Find all anchors, returns a array of element objects
$ret = $html->find('a');

// Find (N)th anchor, returns element object or null if not found (zero based)
$ret = $html->find('a', 0);

// Find lastest anchor, returns element object or null if not found (zero based)
$ret = $html->find('a', -1); 

// Find all 
with the id attribute $ret = $html->find('div[id]'); // Find all
which attribute id=foo $ret = $html->find('div[id=foo]'); // Find all element which id=foo $ret = $html->find('#foo'); // Find all element which class=foo $ret = $html->find('.foo'); // Find all element has attribute id $ret = $html->find('*[id]'); // Find all anchors and images $ret = $html->find('a, img'); // Find all anchors and images with the "title" attribute $ret = $html->find('a[title], img[title]'); // Find all
  • in
      $es = $html->find('ul li'); // Find Nested
      tags $es = $html->find('div div div'); // Find all in which class=hello $es = $html->find('table.hello td'); // Find all td tags with attribite align=center in table tags $es = $html->find(''table td[align=center]');

      Modify HTML Element

      // Get a attribute ( If the attribute is non-value attribute (eg. checked, selected...), it will returns true or false)
      $value = $e->href;
      
      // Set a attribute(If the attribute is non-value attribute (eg. checked, selected...), set it's value as true or false)
      $e->href = 'my link';
      
      // Remove a attribute, set it's value as null! 
      $e->href = null;
      
      // Determine whether a attribute exist? 
      if(isset($e->href)) 
              echo 'href exist!';
      
      // Extract contents from HTML 
      echo $html->plaintext;
      
      // Wrap a element
      $e->outertext = '
      ' . $e->outertext . '
      '; // Remove a element, set it's outertext as an empty string $e->outertext = ''; // Append a element $e->outertext = $e->outertext . '
      foo
      '; // Insert a element $e->outertext = '
      foo
      ' . $e->outertext;

      Dump contents of DOM object in Quick Way

      
      // Dumps the internal DOM tree back into string 
      $str = $html;
      
      // Print it!
      echo $html; 
      

      Dump contents of DOM object in Object oriented Way

      // Dumps the internal DOM tree back into string 
      $str = $html->save();
      
      // Dumps the internal DOM tree back into a file 
      $html->save('result.htm');
      
      

      Customize the parsing behavior “Callback Function”

      // Write a function with parameter "$element"
      function my_callback($element) {
              // Hide all  tags 
              if ($element->tag=='b')
                      $element->outertext = '';
      } 
      
      // Register the callback function with it's function name
      $html->set_callback('my_callback');
      
      // Callback function will be invoked while dumping
      echo $html;
      
      

      The simple code snippets listed above are self explanatory and do not need much detailed explanation.

      • January 3, 2014
      8 Best Free Stock Video Websites for Tiktok, Reels, and Shorts Top 5 PHP Frameworks: Fast and Secure