The wrap()
and unwrap()
methods wrap and unwrap the specified HTML element(s) around each selected element.
$doc->query("[selection]")->wrap("<[tagname] [attributes...]></[tagname]>");
<img src="image.jpg" alt="JumpyDoggy" width="104" height="142">
Wrap with no tag attributes
Php
<?php
include "../src/webparser.php";
$doc = new WebParser();
$doc->loadHTML($html);
$doc->query("img[src='image.jpg']")->wrap("<figure></figure>");
// also possible: $doc->query("img[src='image.jpg']")->wrap("figure");
$doc->output();
Output
<figure>
<img src="image.jpg" alt="JumpyDoggy" width="104" height="142">
</figure>
Wrap with tag attributes
Php
<?php
include "../src/webparser.php";
$doc = new WebParser();
$doc->loadHTML($html);
$doc->query("img[src='image.jpg']")->wrap('<figure class="img-wrapper" style="float: center; display: inline-block;"></figure>');
$doc->output();
Output
<figure class="img-wrapper" style="float: center; display: inline-block;">
<img src="image.jpg" alt="JumpyDoggy" width="104" height="142">
</figure>
$doc->query("[selection]")->unwrap();
<figure class="img-wrapper" style="float: center; display: inline-block;">
<img src="image.jpg" alt="JumpyDoggy" width="104" height="142">
</figure>
Php
<?php
include "../src/webparser.php";
$doc = new WebParser();
$doc->loadHTML($html);
$doc->query("img[src='image.jpg']")->unwrap();
$doc->output();
Output
<img src="image.jpg" alt="JumpyDoggy" width="104" height="142">
Click here to go to example test.