Php simple html dom parser example

PHP Simple HTML DOM Parser

A fast, simple and reliable HTML document parser for PHP.

Created by S.C. Chen, based on HTML Parser for PHP 4 by Jose Solorzano.

Parse any HTML document

PHP Simple HTML DOM Parser handles any HTML document, even ones that are considered invalid by the HTML specification.

Select elements using CSS selectors

PHP Simple HTML DOM Parser supports CSS style selectors to navigate the DOM, similar to jQuery.

Download

  • Download the latest version from SourceForge

Contributing

  • Request features on the Feature Request Tracker
  • Report bugs on the Bug Tracker
  • Get involved with the community on the Discussions Board

License

PHP Simple HTML DOM Parser is Free Software licensed under the MIT License.


I'm having trouble figuring out how to use PHP Simple HTML DOM Parser for pulling information from a website.

require('simple_html_dom.php');
$html = file_get_html('https://example.com');

$ret = array();
foreach($html->find(".project-card-mini-wrap") as $element)  { 
   echo $element;   
}

The output of $element is:


This is the information I'd like to pull from the website:
1: Link href
2: Image src
3: Project name

jaggedsoft

3,5871 gold badge32 silver badges40 bronze badges

asked Nov 20, 2015 at 8:14

Php simple html dom parser example

3

Hopefully this will provide some insight to you as well as other users of PHP Simple HTML DOM Parser

foreach($html->find(".project-card-mini-wrap") as $element)  { 
   echo "Project name: ",$element->find('.project_name',0)->innertext,"
\n"; echo "Image source: ",$element->find('img',0)->src,"
\n"; echo "Link: ",$element->find('a',0)->href,"
\n"; }

Produces this output:

Project name: KOSTIREV - THE REAL YOU 
Image source: https://ksr-ugc.imgix.net/projects/2123706/photo-original.png?v=1444253259&w=218&h=162&fit=crop&auto=format&q=92&s=9d6c437e96b720dce82fc9b598b3e8ae
Link: /projects/andrewkostirev/kostirev-the-real-you

answered Nov 20, 2015 at 8:23

jaggedsoftjaggedsoft

3,5871 gold badge32 silver badges40 bronze badges

2

I tried this and it worked, thanks for the help! Here is something i made using primewire.ag as a example.... The goal here was to extract all the links of a given page.

find(".movie_version_link") as $linkClass)  {
    echo "Link: ",$linkPrefix,$linkClass->find('a',0)->href,"
\n"; } ?>

answered Feb 3, 2018 at 3:25

Not the answer you're looking for? Browse other questions tagged php html dom or ask your own question.

What is simple HTML DOM parser PHP?

A simple PHP HTML DOM parser written in PHP5+, supports invalid HTML, and provides a very easy way to find, extract and modify the HTML elements of the dom. jquery like syntax allow sophisticated finding methods for locating the elements you care about.

What is simple HTML DOM?

The HTML DOM is an Object Model for HTML. It defines: HTML elements as objects. Properties for all HTML elements. Methods for all HTML elements.

What is HTML DOM parser?

The DOMParser interface provides the ability to parse XML or HTML source code from a string into a DOM Document . You can perform the opposite operation—converting a DOM tree into XML or HTML source—using the XMLSerializer interface.

How parse HTML in PHP?

To add the dynamic data (HTML content) at a certain point in PHP code, we need parsing. For example: For adding the data (info) in the form of HTML, we need to make that dynamic template in string and then convert it to HTML. How should we do parsing? We should use loadHTML() function for parsing.