https://github.com/devvoh/simpledom
SimpleDom adds simple class manipulation to DOMDocuments and DOMElements.
https://github.com/devvoh/simpledom
Last synced: 3 months ago
JSON representation
SimpleDom adds simple class manipulation to DOMDocuments and DOMElements.
- Host: GitHub
- URL: https://github.com/devvoh/simpledom
- Owner: devvoh
- License: mit
- Created: 2017-12-30T21:54:25.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-31T09:31:28.000Z (about 8 years ago)
- Last Synced: 2024-03-15T09:02:52.943Z (almost 2 years ago)
- Language: PHP
- Size: 9.77 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SimpleDom
[](https://travis-ci.org/devvoh/simpledom)
[](https://packagist.org/packages/devvoh/simpledom)
[](https://packagist.org/packages/devvoh/simpledom)
[](https://packagist.org/packages/devvoh/simpledom)
SimpleDom adds simple class manipulation to DOMDocuments and DOMElements.
## Requirements
- PHP 5.6+
- Composer
## Installation
SimpleDom can be installed by using [Composer](http://getcomposer.org/). Simply run:
`composer require devvoh/simpledom`
## Usage
SimpleDom can be considered a facade adapter for the built-in `DOMDocument` and `DOMElement` classes. As such, you can use them anywhere you currently use those.
Creating a new instance is done with an extra step, however:
```php
$domDocument = new \DOMDocument();
$domDocument->loadHTML($htmlString);
$document = \SimpleDom\Document::fromDOMDocument($domDocument);
```
To then get all elements with the class "header":
```php
$elements = $document->getElementsByClassName("header");
```
And you'll get an array of `\SimpleDom\Element` items. SimpleDom also overwrites the following `DOMDocument` methods: `getElementsByTagName()`, `getElementById()` and `createElement()`.
Normally the `getElement` methods would return a `DOMNodeList` but SimpleDom returns an array of elements instead.
It's also possible to get elements which have multiple classes.
```php
$elements = $document->getElementsByClassNames(["header", "sub"]);
```
Which will only return items that have both the "header" and "sub" class. The order of which is not important.
To get the DOMDocument back so you can save the HTML, simply do this:
```php
$domDocument = $document->getDOMDocument();
echo $domDocument->saveHTML();
```
SimpleDom `Element` instances have some added features as well.
```php
$element = $document->createElement("span", "this is a span!");
$element->addClass("blue");
```
The above code will result in the following html: `this is a span!`
The following methods are available to work with classes and Elements:
- `getClasses(): array`
- `setClasses(array): Element`
- `hasClass(string): bool`
- `hasClasses(array): bool`
- `addClass(string): Element`
- `addClasses(array): Element`
- `removeClass(string): Element`
- `removeClasses(array): Element`
- `toggleClass(string): Element`
- `toggleClasses(array): Element`
- `clearClasses(): Element`
## Contact
All questions can be asked through github. Just create an issue and I'll get back with an answer as soon as possible.
## License
Devvoh SimpleDom is open-sourced software licensed under the MIT license.