Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nadiamoe/betterdomdocument
Extension of PHP's native DOMDocument with some extra functions, including searching by attributes and its values.
https://github.com/nadiamoe/betterdomdocument
Last synced: 24 days ago
JSON representation
Extension of PHP's native DOMDocument with some extra functions, including searching by attributes and its values.
- Host: GitHub
- URL: https://github.com/nadiamoe/betterdomdocument
- Owner: nadiamoe
- Created: 2014-11-30T04:54:39.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2014-12-02T16:54:47.000Z (about 10 years ago)
- Last Synced: 2024-12-27T05:04:21.688Z (25 days ago)
- Language: PHP
- Size: 113 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# BetterDOMDocument
BetterDOMDocument is a simple class which extends PHP's native class DOMDocument, and provides extra functions:
* Filter elements not only by tag, but also by attribute and its value.
* Returning HTML contained in the node.
## Examples
### Create new object and load HTML file
loadHTMLFile('http://www.google.com', LIBXML_NOWARNING | LIBXML_NOERROR);
### Find links (a) whose 'class' is exactly 'gbmt'.
$links = $dom->getElementsByTagAttribute('a', 'class', 'gbmt');
// Print those links with its class.
echo "Links with class=\"gbmt\"\n";
echo "#######################\n";foreach ($links as $link) {
echo $link->getAttribute('class') .': '. $link->getAttribute('href') . "\n";
}echo "\n";
### Find the first link to an HTTPs server (links which start with 'https').
// The fourth parameter specifies the index of the element to return.
// If zero is supplied, this method returns the first matching DOMNode instead of a collection.
$link = $dom->getElementsByTagAttribute('a', 'href', '/^https/', 0);// Print this link with its class.
echo "HTTPs link\n";
echo "##########\n";
echo $link->getAttribute('class') .': '. $link->getAttribute('href') . "\n";echo "\n";
### Find the second link with class="gbzt".
$link = $dom->getElementsByTagAttribute('a', 'class', 'gbzt', 1);
// Print link.
echo "Sencond link with class=\"gbzt\"\n";
echo "##############################\n";
echo $link->getAttribute('class') .': '. $link->getAttribute('href') . "\n";echo $dom->getInnerText($link);
echo "\n";
?>