Skip to content

Latest commit

 

History

History
78 lines (62 loc) · 1.77 KB

remove-and-clear.md

File metadata and controls

78 lines (62 loc) · 1.77 KB

remove and clear

<< Previous                                                              Next >>

To remove elements and content, there are mainly two methods:

remove() - Removes the selected element (and its child elements) clear() - Removes the child elements from the selected element

Syntax: remove and clear

$doc->Q([selector])->remove();
$doc->Q([selector])->clear();

$html

<div id="div1" 
style="height:100px;width:300px;
border:1px solid black;background-color:yellow;">
  This is some text in the div.
  <p>This is a paragraph in the div.</p>
  <p>This is another paragraph in the div.</p>
</div>
<div id="div2" 
style="height:100px;width:300px;
border:1px solid black;background-color:yellow;">
  This is some text in the div.
  <p>This is a paragraph in the div.</p>
  <p>This is another paragraph in the div.</p>
</div>
Php
<?php
include "../src/webparser.php";
$doc = new WebParser();
$doc->loadHTML($html);

$doc->Q("#div1")->remove();
$doc->Q("#div2")->clear();

$doc->output();
Output
<div id="div2" style="height:100px;width:300px;border:1px solid black;
  background-color:yellow;">
</div>

Example test code snippet

Click here to go to example test.