PHP code example of amsify42 / php-domfinder

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);

$domFinder = get_dom_finder('http://www.site.com/file.html', 'html', true);

$domFinder->dom();	

$domFinder->dom()->getElementsByTagName('p');	

$domFinder->finder();

$domFinder->finder()->query("/div[@class='body-entry']");	

$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);

$metaTags = $domFinder->metaTags();

$title = $domFinder->getMetaValue('name', 'title');

$title = $domFinder->getMetaValue('name', 'title', 'myattr');

$paras = $domFinder->getElements('p');

$para = $domFinder->getFirstElement('p');

$para = $domFinder->getElement('p', 1);

$elements = $domFinder->findByClass('section-items')->all();

$element = $domFinder->findByClass('section-items')->first();
// or
$element = $domFinder->findFirstByClass('section-items');

$elements = $domFinder->find('div')->byClass('section-items')->all();

$element = $domFinder->find('div')->byClass('section-items')->first();

$element = $domFinder->find('div')->byClass('section-items')->get(1); // This will return 2nd element

$elements = $domFinder->findClassLike('section-items')->all();

$element = $domFinder->findClassLike('section-items')->first();
// or
$element = $domFinder->findFirstClassLike('section-items');

$divs = $domFinder->find('div')->classLike('section-items')->all();

$div = $domFinder->find('div')->classLike('section-items')->first();

$div = $domFinder->find('div')->classLike('section-items')->get(1); // This will return 2nd element

$elements = $domFinder->findById('body-entry')->all();

$element = $domFinder->findById('body-entry')->first();
// or
$element = $domFinder->findFirstById('body-entry');

$divs = $domFinder->find('div')->byId('body-entry')->all();

$div = $domFinder->find('div')->byId('body-entry')->first();

$elements = $domFinder->findIdLike('section-')->all();

$element = $domFinder->findIdLike('section-')->first();
// or
$element = $domFinder->findFirstIdLike('section-');

$divs = $domFinder->find('div')->idLike('section-')->all();

$div = $domFinder->find('div')->idLike('section-')->first();

$div = $domFinder->find('div')->idLike('section-')->get(1); // This will return 2nd element

$elements = $domFinder->findByAttr('data-section', 'paragraph')->all();

$element = $domFinder->findByAttr('data-section', 'paragraph')->first();
// or
$element = $domFinder->findFirstByAttr('data-section', 'paragraph');

$divs = $domFinder->find('div')->byAttr('data-section', 'paragraph')->all();

$div = $domFinder->find('div')->byAttr('data-section', 'paragraph')->first();

$div = $domFinder->find('div')->byAttr('data-section', 'paragraph')->get(1); // This will return 2nd element

$elements = $domFinder->findAttrLike('my-att', 'some-')->all();

$element = $domFinder->findAttrLike('my-att', 'some-')->first();
// or
$element = $domFinder->findFirstAttrLike('my-att', 'some-');

$divs = $domFinder->find('div')->attrLike('my-att', 'some-')->all();

$div = $domFinder->find('div')->attrLike'my-att', 'some-')->first();

$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
}

$data = $section->extractByRegex(["/<some-element(.*?)some-element>/", "/class=\"some-class\">(.*?)\<\//"], true);

$ul = $domFinder->getElement('ul');
// or
$ul = $domFinder->findFirst('ul');

echo $ul->outerHTML();

echo $ul->innerHTML();

$uls = $domFinder->find('div')->byClass('child-class')->find('ul')->all();
// or
$uls = $domFinder->find('div')->byClass('child-class')->findAll('ul');

$uls = $domFinder->finder()->query("/div[@class='child-class']/ul");

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