PHPackages                             arc/html - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. arc/html

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

arc/html
========

Ariadne Component Library: html writer and parser Component

3.0(6y ago)030631MITPHPPHP &gt;=7.1CI failing

Since Jan 17Pushed 5y ago2 watchersCompare

[ Source](https://github.com/Ariadne-CMS/arc-html)[ Packagist](https://packagist.org/packages/arc/html)[ Docs](https://github.com/Ariadne-CMS/arc/wiki)[ RSS](/packages/arc-html/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (5)Dependencies (3)Versions (6)Used By (1)

ARC: Ariadne Component Library
==============================

[](#arc-ariadne-component-library)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/1eee71dfce29a9862a1a2257a5e187fb5411c4c691b6d70a7c7dabd705b9eac7/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f41726961646e652d434d532f6172632d68746d6c2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Ariadne-CMS/arc-html/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/5e0f6e7b01c3425a9c205d11bfbedd0e42201fb5c81bd248e080cd917fc3ac26/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f41726961646e652d434d532f6172632d68746d6c2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Ariadne-CMS/arc-html/)[![Latest Stable Version](https://camo.githubusercontent.com/17d6667c0aeb3284336f445399ffc3d232a823b54b3708cbcb724c69085aeb6b/68747470733a2f2f706f7365722e707567782e6f72672f6172632f68746d6c2f762f737461626c652e737667)](https://packagist.org/packages/arc/html)[![Total Downloads](https://camo.githubusercontent.com/61768aa0f6e7100b04b6f617c9ec1adef9714dc43ff2ed245b30fb512c974666/68747470733a2f2f706f7365722e707567782e6f72672f6172632f786d6c2f646f776e6c6f6164732e737667)](https://packagist.org/packages/arc/html)[![Latest Unstable Version](https://camo.githubusercontent.com/13c5e12fbe85e9a17d4f84cf3de09a019a73587305d3e7c08897a3742b66237a/68747470733a2f2f706f7365722e707567782e6f72672f6172632f68746d6c2f762f756e737461626c652e737667)](https://packagist.org/packages/arc/html)[![License](https://camo.githubusercontent.com/038eb27b56766a201dcc172c3451768910710ab52275ef5552890b3e3fc546fe/68747470733a2f2f706f7365722e707567782e6f72672f6172632f68746d6c2f6c6963656e73652e737667)](https://packagist.org/packages/arc/html)

arc/html
========

[](#archtml)

This component provides a unified html parser and writer. The writer allows for readable and correct html in code, not using templates. The parser is a wrapper around both DOMDocument and SimpleXML.

The parser and writer also work on fragments of HTML. The parser also makes sure that the output is identical to the input. When converting a node to a string, \\arc\\html will return the full html string, including tags. If you don't want that, you can always access the 'nodeValue' property to get the original SimpleXMLElement.

Finally the parser also adds the ability to use basic CSS selectors to find elements in the HTML.

```
	use \arc\html as h;
	$htmlString = h::doctype()
	 .h::html(
	 	h::head(
	 		h::title('Example site')
	 	),
	 	h::body(
	 		['class' => 'homepage'],
	 		h::h1('An example site')
	 	)
	 );
```

```
	$html = \arc\html::parse($htmlString);
	$title = $html->head->title->nodeValue; // SimpleXMLElement 'Example site'
	$titleTag = $html->head->title; // Example site
```

CSS selectors
-------------

[](#css-selectors)

```
	$title = current($html->find('title'));
```

The find() method always returns an array, which may be empty. By using current() you get the first element found, or null if nothing was found.

The following CSS selectors are supported:

- `tag1 tag2`
    This matches `tag2` which is a descendant of `tag1`.
- `tag1 > tag2`
    This matches `tag2` which is a direct child of `tag1`.
- `tag:first-child`
    This matches `tag` only if its the first child.
- `tag1 + tag2`
    This matches `tag2` only if its immediately preceded by `tag1`.
- `tag1 ~ tag2`
    This matches `tag2` only if it has a previous sibling tag1.
- `tag[attr]`
    This matches `tag` if it has the attribute `attr`.
- `tag[attr="foo"]`
    This matches `tag` if it has the attribute `attr` with the value `foo` in its value list.
- `tag#id`
    This matches any `tag` with id `id`.
- `#id`
    This matches any element with id `id`.
- `tag.class-name`
    Matches any `tag` with a class `class-name`.
- `.class-name`
    Matches any element with a class `class-name`.

SimpleXML
---------

[](#simplexml)

The parsed HTML behaves almost identical to a SimpleXMLElement, with the exceptions noted above. So you can access attributes just like SimpleXMLElement allows:

```
	$class = $html->html->body['class'];
	$class = $html->html->body->attributes('version');
```

You can walk through the node tree:

```
	$title = $html->html->head->title;
```

Any method or property available in SimpleXMLElement is included in \\arc\\html parsed data.

DOMElement
----------

[](#domelement)

In addition to SimpleXMLElement methods, you can also call any method and most properties available in DOMElement.

```
	$class = $html->html->body->getAttributes('class');
	$title = current($html->getElementsByTagName('title'));
```

Parsing fragments
-----------------

[](#parsing-fragments)

The arc\\html parser also accepts partial HTML content. It doesn't require a single root element.

```
    $htmlString =
