Skip to content

Latest commit

 

History

History
77 lines (57 loc) · 1.75 KB

html-and-text.md

File metadata and controls

77 lines (57 loc) · 1.75 KB

html and text

<< Previous                                                              Next >>

The html() method sets or returns the content (innerHTML) of the selected elements.

When this method is used to return content, it returns the content of the FIRST matched element.

When this method is used to set content, it overwrites the content of ALL matched elements.

Tip: text() works the same way as html(), except that it is used to set or return only text content.

Syntax: html and text

Return content:

$doc->Q([selector])->html();
$doc->Q([selector])->text();

Set content:

$doc->Q([selector])->html([html-content]);
$doc->Q([selector])->text([text-content]);

$html

<p>This is a paragraph</p>
<p>This is another paragraph</p>
Php
```php
<?php
include "../src/webparser.php";
$doc = new WebParser();
$doc->loadHTML($html);

$doc->Q("p:first")->html("<i>Hello world!</i>");
$text = $doc->Q("p:first")->text();
$doc->Q("p[2]")->text("<b>$text</b>");

$doc->output();
```
Output
```html
<p><i>Hello world!</i></p>
<p>&lt;b&gt;this is a paragraph&lt;/b&gt;</p> 
```

Example test code snippet

Click here to go to example test.