| 1 |
--- |
| 2 |
title: API Reference |
| 3 |
--- |
| 4 |
|
| 5 |
# Parsing documents |
| 6 |
|
| 7 |
The parser accepts documents in the form of URLs, files and strings. The document |
| 8 |
must be accessible for reading and cannot exceed [](constants.md#max_file_size`MAX_FILE_SIZE`](constants.md#max_file_size](constants.md#max_file_size). |
| 9 |
|
| 10 |
Name | Description |
| 11 |
---- | ----------- |
| 12 |
`str_get_html( string $content ) : object` | Creates a DOM object from string. |
| 13 |
`file_get_html( string $filename ) : object` | Creates a DOM object from file or URL. |
| 14 |
|
| 15 |
# DOM methods & properties |
| 16 |
|
| 17 |
Name | Description |
| 18 |
---- | ----------- |
| 19 |
`__construct( [string $filename] ) : void` | Constructor, set the filename parameter will automatically load the contents, either text or file/url. |
| 20 |
`plaintext : string` | Returns the contents extracted from HTML. |
| 21 |
`clear() : void` | Clean up memory. |
| 22 |
`load( string $content ) : void` | Load contents from string. |
| 23 |
`save( [string $filename] ) : string` | Dumps the internal DOM tree back into a string. If the $filename is set, result string will save to file. |
| 24 |
`load_file( string $filename ) : void` | Load contents from a file or a URL. |
| 25 |
`set_callback( string $function_name ) : void` | Set a callback function. |
| 26 |
`find( string $selector [, int $index] ) : mixed` | Find elements by the CSS selector. Returns the Nth element object if index is set, otherwise return an array of object. |
| 27 |
|
| 28 |
# Element methods & properties |
| 29 |
|
| 30 |
Name | Description |
| 31 |
---- | ----------- |
| 32 |
`[attribute] : string` | Read or write element's attribute value. |
| 33 |
`tag : string` | Read or write the tag name of element. |
| 34 |
`outertext : string` | Read or write the outer HTML text of element. |
| 35 |
`innertext : string` | Read or write the inner HTML text of element. |
| 36 |
`plaintext : string` | Read or write the plain text of element. |
| 37 |
`find( string $selector [, int $index] ) : mixed` | Find children by the CSS selector. Returns the Nth element object if index is set, otherwise return an array of object. |
| 38 |
|
| 39 |
# DOM traversing |
| 40 |
|
| 41 |
Name | Description |
| 42 |
---- | ----------- |
| 43 |
`$e->children( [int $index] ) : mixed` | Returns the Nth child object if index is set, otherwise return an array of children. |
| 44 |
`$e->parent() : element` | Returns the parent of element. |
| 45 |
`$e->first_child() : element` | Returns the first child of element, or null if not found. |
| 46 |
`$e->last_child() : element` | Returns the last child of element, or null if not found. |
| 47 |
`$e->next_sibling() : element` | Returns the next sibling of element, or null if not found. |
| 48 |
`$e->prev_sibling() : element` | Returns the previous sibling of element, or null if not found. |
| 49 |
|
| 50 |
# Camel naming conventions |
| 51 |
|
| 52 |
Method | Mapping |
| 53 |
------ | ------- |
| 54 |
`$e->getAllAttributes()` | `$e->attr` |
| 55 |
`$e->getAttribute( $name )` | `$e->attribute` |
| 56 |
`$e->setAttribute( $name, $value)` | `$value = $e->attribute` |
| 57 |
`$e->hasAttribute( $name )` | `isset($e->attribute)` |
| 58 |
`$e->removeAttribute ( $name )` | `$e->attribute = null` |
| 59 |
`$e->getElementById ( $id )` | `$e->find ( "#$id", 0 )` |
| 60 |
`$e->getElementsById ( $id [,$index] )` | `$e->find ( "#$id" [, int $index] )` |
| 61 |
`$e->getElementByTagName ($name )` | `$e->find ( $name, 0 )` |
| 62 |
`$e->getElementsByTagName ( $name [, $index] )` | `$e->find ( $name [, int $index] )` |
| 63 |
`$e->parentNode ()` | `$e->parent ()` |
| 64 |
`$e->childNodes ( [$index] )` | `$e->children ( [int $index] )` |
| 65 |
`$e->firstChild ()` | `$e->first_child ()` |
| 66 |
`$e->lastChild ()` | `$e->last_child ()` |
| 67 |
`$e->nextSibling ()` | `$e->next_sibling ()` |
| 68 |
`$e->previousSibling ()` | `$e->prev_sibling ()` |