Free Business Consulting

PHP simple HTML DOM Parser Script

adminPHP0 Comments

Simple HTML DOM parser is a PHP 5+ class. It is useful to manipulate HTML elements. This class can work with both valid HTML and HTML class that didn’t pass W3C validation. You can find elements by ids, classes, tags and many more. You can also add, delete or alter DOM elements. The only one thing you should care about is memory leaks. But you can avoid memory leaks.

Get Started with PHP Simple HTML DOM Parser

After uploading the class file, the simple HTML DOM class instance has to be created.

There are three ways to create DOM class:

  1. Load HTML from a file
  2. Load HTML from a URL
  3. Load HTML from a string

If you use “Load HTML from a string” and want more control over HTTP request, then use CURL to fetch HTML to a string and after that load the DOM class object from a string.

Find HTML Elements using PHP Simple HTML DOM Parser

You can use the find function to find HTML DOM elements. It returns an object or an array of an object.


Use “selectors” to find DOM Elements:

Parent, child and sibling elements selection using built-in functions:

Attribute Operators

With simple regular expressions, we can use attribute selectors.

  • [attribute] – Select HTML DOM elements that have a certain attribute
  • [attribute=value] – elements which have the specified attribute with a specific value.
  • [attribute!=value]- elements which don’t have the specified attribute with a specific value.
  • [attribute*=value] – elements with the particular attribute whose value contains the specified value
  • [attribute$=value] – elements with the specified attribute whose value ends with the specified value
  • [attribute^=value] – elements with the specified attribute whose value begins with the certain

Accessing DOM Element Attributes with PHP Simple HTML DOM Parser

Attributes are actually object variables:

Each object has four attributes:

  1. tag – returns the tag name
  2. innertext – returns inner HTML of an element
  3. outertext – returns outer HTML of an element
  4. plaintext – returns plain text (without HTML tags)

Editing HTML Elements with PHP Simple HTML DOM Parser

Edit an attribute is similar to reading their values.

There are no special functions to append or remove elements, but there are some methods:

To save the DOM document just put the DOM object into a variable:

Prevent PHP Simple HTML DOM Parser Memory Leak

Always be careful about memory leak because it can slow own your website. You can add following lines to avoid memory leaks.

Happy Coding!!

Do you want professional PHP Web Development services?

 PHP development services

Leave a Reply

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

Get News In Your Inbox

Popular Blogs

How to create callback functions for jQuery plugins & extensions?

Most of the time custom jQuery plugins and extensions that we create do not use callback functions. They usually simply work on DOM elements or do some calculations. But there…

Continue Reading
Top 5 Best Ecommerce plugins in WordPress

Nowadays online shopping has increased and become very convenient shopping place for users, you may be wanting to start your own online shop so you can unleash your products to…

Continue Reading
10 Best WordPress Live Chat Plugins for Customer Support

The Live chat is the common thing now days, if you own any eCommerce website or if you are the service provider. It makes easy for your customers to contact…

Continue Reading
Free plagiarism checker sites

Plagiarism is the process of taking someone’s ideas & work and passing them off as one’s own. Prevent Plagiarism to check originality of content is the most important factor for…

Continue Reading
How to show wordpress posts in other CMS?

Currently since from few weeks, I am working on Expression Engine. Now you thought that what is Expression Engine. Now let me explain you in little brief. Expression Engine is…

Continue Reading

Remarkable Facts About Us

0 %
Client satisfaction
0 +
Years of Experience
0 +
Happy Clients
Projects Accomplished

Ready to Get Started ?