h0gar/xpath

0.1.4 2016-01-04 13:47 UTC

This package is not auto-updated.

Last update: 2024-12-21 16:20:44 UTC


README

#Xpath

Build Status

A lightweight package for xpath manipulations.

##Installation

composer require h0gar/xpath *

##Usage

HTML

$html = file_get_contents('page.html');
$doc = new \H0gar\Xpath\Doc($html, 'html');

XML

$xml = file_get_contents('page.xml');
$doc = new \H0gar\Xpath\Doc($xml, 'xml');

##Access a node

$item = $doc->item('/html/body/div[1]/div/div[2]/ul/li[0]');
#or
$item = $doc->item('/html/body/div[1]')->item('div/div[2]/ul/li', 1);

##Access multiple nodes

$items = $doc->items('/html/body/div[1]/div/div[2]/ul/li');
foreach($items as $item)
	//...

##Navigation

$next = $item->next();
$prev = $item->prev();
$parent = $item->parent();

##Get a node inner html

$html = $item->html();
#or
$html = $doc->html('/html/body/div[1]/div/div[2]/ul/li[0]');
#or
$html = $doc->html('/html/body/div[1]/div/div[2]/ul/li', 1);

##Get a node inner text

$text = $item->text();
#or
$text = $doc->text('/html/body/div[1]/div/div[2]/ul/li[0]');
#or
$text = $doc->text('/html/body/div[1]/div/div[2]/ul/li', 1);

##Get an attribute

$href = $item->attr('href');

##\DOMElement and \DOMXpath

$domelement = $item->getNode();
$domxpath = $item->getXpath();

##Contributing

Please submit all issues and pull requests to the h0gar/xpath repository.

##License

Open-sourced software licensed under the MIT license