1. Go to this page and download the library: Download amsify42/php-domfinder 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/ */
amsify42 / php-domfinder example snippets
$domFinder = new Amsify42\DOMFinder\DOMFinder('path/to/file.html');
// or
$domFinder = new Amsify42\DOMFinder\DOMFinder();
$domFinder->load('path/to/file.html');
$domFinder = new Amsify42\DOMFinder\DOMFinder('path/to/file.html', 'html');
// or
$domFinder = new Amsify42\DOMFinder\DOMFinder();
$domFinder->loadHTML('path/to/file.html');
$domFinder = new Amsify42\DOMFinder\DOMFinder('path/to/file.xml', 'xml');
// or
$domFinder = new Amsify42\DOMFinder\DOMFinder();
$domFinder->loadXML('path/to/file.xml');
$domFinder = new Amsify42\DOMFinder\DOMFinder('http://www.site.com/file.html', 'html', true);
// or
$domFinder = new Amsify42\DOMFinder\DOMFinder();
$domFinder->loadHTML('http://www.site.com/file.html', true);
$domFinder = new Amsify42\DOMFinder\DOMFinder('http://www.site.com/file.xml', 'xml', true);
// or
$domFinder = new Amsify42\DOMFinder\DOMFinder();
$domFinder->loadXML('http://www.site.com/file.xml', true);
$anchors = $domFinder->find('a')->byClass('action-link')->all();
if($anchors->length)
{
foreach($anchors as $anchor)
{
var_dump($anchor); // Will be of type Amsify42\DOMFinder\DOM\Element which extends DOMElement
}
}
foreach($anchors as $anchor)
{
$anchor->getAttribute('href');
}
$para = $domFinder->getFirstElement('p');
// or
$para = $domFinder->getElement('p', 1);
// or
$para = $domFinder->findFirst('p');
// or
$para = $domFinder->find('p')->first();
// or
$para = $domFinder->find('p')->get(1);
$div = $domFinder->find('div')->attrLike('my-att', 'some-')->get(1); // This will return 2nd element
$html = '<div class="section">
<script>var data={"name": "my name", "id":12345};</script>
</div>';
$domFinder = new Amsify42\DOMFinder\DOMFinder();
$domFinder->loadHTML($html);
$section = $domFinder->findFirstByClass('section');
if($section)
{
$data = $section->extractByRegex("/data\=(.*?)\;</"); // Here you will get js dictionary data
}
$html = '<div class="section">
<some-element class="some-class">{"name": "name one", "id":1}</some-element>
<some-element class="some-class">{"name": "name two", "id":2}</some-element>
<some-element class="some-class">{"name": "name three", "id":3}</some-element>
</div>';
$domFinder = new Amsify42\DOMFinder\DOMFinder();
$domFinder->loadHTML($html);
$section = $domFinder->findFirstByClass('section');
if($section)
{
$data = $section->extractByRegex("/class=\"some-class\">(.*?)\<\//", true); // Here you will get multiple js dictionary data as array
}
if($uls->length)
{
foreach($uls as $ul)
{
var_dump($ul);
}
}
$div = $domFinder->find('div')->byClass('parent-class')->first();
if($div)
{
$divs = $div->find('div')->byClass('child-class')->all(); // At this level DOMFinder instance will be created and assigned to this element
if($divs->length)
{
echo $divs->length;
}
}
$ composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.