1. Go to this page and download the library: Download gm314/diavazo library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
gm314 / diavazo example snippets
use Diavazo\HTMLDocument;
$document = new HTMLDocument();
// load file
$document->loadFile("local.html");
$document->loadFile("http://mypage.com/test.html");
// load from string
$document->loadString("<html></html>");
$document = new HTMLDocument();
$document->loadFile(__DIR__ . "/assets/TableToArrayTest.html");
// get element by id
$table = $document->getElementById("associateArrayTest");
// get element by tag name
$elementList = $document->getElementByTagName("div");
// find all <p> elements, all elements with the class 'spanClass' and all <b class="bClass">
$elementList = $document->getElement("p .spanClass b.bClass");
// xpath query
$title = $document->query("/html/head/title");
// get root (<html>)
$root = $document->getRootElement();
$document = new HTMLDocument();
$document->loadFile(__DIR__ . "/assets/TableToArrayTest.html");
$table = $document->getElementById("table");
// will return the first tr (Breadth-first search)
$table->getFirstDescendantByName("tr");
// will return all td and th elements
$tdList = $table->getDescendantByName("td th");
// will find all elements that have the class 'active'
$root = $document->getRootElement();
$elementsWithClass = $root->getDescendantWithClassName("active");
// will find all elements that have the class 'myClass' and are td or th elements
$elementsWithClass = $root->getDescendantWithClassName("myClass", "td th");
// will find all elements having only the class 'testClass'
$elementsWithExactClass = $root->getDescendantWithClassNameStrict("testClass");
// will find all elements having only the class 'testClass' and are td or th elements
$elementsWithExactClass = $root->getDescendantWithClassNameStrict("testClass", "td th");
// find all <p> elements, all elements with the class 'spanClass' and all <b class="bClass"> that are descendants of #myId
$anyElement = $document-getElementById("myId");
$elementList = $document->getElement("p .spanClass b.bClass");
$document = new HTMLDocument();
$document->loadFile("myFile.html");
$table = $document->getElementBy("myTable");
// will return null if the attribute does not exist otherwise string
$table->getAttributeValue("align");