| 1 |
# Quick Start |
| 2 |
|
| 3 |
Find below sample code that demonstrate the fundamental features of PHP Simple |
| 4 |
HTML DOM Parser. |
| 5 |
|
| 6 |
## Read plain text from HTML document |
| 7 |
|
| 8 |
```php |
| 9 |
echo file_get_html('https://www.google.com/')->plaintext; |
| 10 |
``` |
| 11 |
|
| 12 |
Loads the specified HTML **document** into memory, parses it and returns the |
| 13 |
plain text. Note that [](api/api.md`file_get_html`](api/api.md](api/api.md) supports local files as well |
| 14 |
as remote files! |
| 15 |
|
| 16 |
## Read plaint text from HTML string |
| 17 |
|
| 18 |
```php |
| 19 |
echo str_get_html('<ul><li>Hello, World!</li></ul>')->plaintext; |
| 20 |
``` |
| 21 |
|
| 22 |
Parses the provided HTML **string** and returns the plain text. Note that the |
| 23 |
parser handles partial documents as well as full documents. |
| 24 |
|
| 25 |
## Read specific elements from HTML document |
| 26 |
|
| 27 |
```php |
| 28 |
$html = file_get_html('https://www.google.com/'); |
| 29 |
|
| 30 |
foreach($html->find('img') as $element) |
| 31 |
echo $element->src . '<br>'; |
| 32 |
|
| 33 |
foreach($html->find('a') as $element) |
| 34 |
echo $element->href . '<br>'; |
| 35 |
``` |
| 36 |
|
| 37 |
Loads the specified document into memory and returns a list of image sources as |
| 38 |
well as anchor links. Note that [](manual/finding-html-elements.md`find`](manual/finding-html-elements.md](manual/finding-html-elements.md) |
| 39 |
supports [](https://www.w3.org/TR/selectors/CSS](https://www.w3.org/TR/selectors/](https://www.w3.org/TR/selectors/) selectors to find elements in |
| 40 |
the DOM. |
| 41 |
|
| 42 |
## Modify HTML documents |
| 43 |
|
| 44 |
```php |
| 45 |
$doc = '<div id="hello">Hello, </div><div id="world">World!</div>'; |
| 46 |
|
| 47 |
$html = str_get_html($doc); |
| 48 |
|
| 49 |
$html->find('div', 1)->class = 'bar'; |
| 50 |
$html->find('div[id=hello]', 0)->innertext = 'foo'; |
| 51 |
|
| 52 |
echo $html; // <div id="hello">foo</div><div id="world" class="bar">World!</div> |
| 53 |
``` |
| 54 |
|
| 55 |
Parses the provided HTML string and replaces elements in the DOM before returning |
| 56 |
the updated HTML string. In this example, the class for the second `div` element |
| 57 |
is set to `bar` and the inner text for the first `div` element to `foo`. |
| 58 |
|
| 59 |
Note that [`find`](manual/finding-html-elements.md) supports a second parameter |
| 60 |
to return a single element from the array of matches. |
| 61 |
|
| 62 |
Note that attributes can be accessed directly by the means of magic methods |
| 63 |
(`->class` and `->innertext` in the example above). |
| 64 |
|
| 65 |
## Collect information from Slashdot |
| 66 |
|
| 67 |
```php |
| 68 |
$html = file_get_html('https://slashdot.org/'); |
| 69 |
|
| 70 |
$articles = $html->find('article[data-fhtype="story"]'); |
| 71 |
|
| 72 |
foreach($articles as $article) { |
| 73 |
$item['title'] = $article->find('.story-title', 0)->plaintext; |
| 74 |
$item['intro'] = $article->find('.p', 0)->plaintext; |
| 75 |
$item['details'] = $article->find('.details', 0)->plaintext; |
| 76 |
$items[] = $item; |
| 77 |
} |
| 78 |
|
| 79 |
print_r($items); |
| 80 |
``` |
| 81 |
|
| 82 |
Collects information from [](https://slashdot.org/Slashdot](https://slashdot.org/](https://slashdot.org/) for further processing. |
| 83 |
|
| 84 |
Note that the combination of CSS selectors and magic methods make the process of |
| 85 |
parsing HTML documents a simple task that is easy to understand. |