Skip to content

Latest commit

 

History

History
110 lines (82 loc) · 2.33 KB

wrap-and-unwrap.md

File metadata and controls

110 lines (82 loc) · 2.33 KB

wrap and unwrap

<< Previous                                                              Next >>

The wrap() and unwrap() methods wrap and unwrap the specified HTML element(s) around each selected element.

Syntax: wrap

$doc->query("[selection]")->wrap("<[tagname] [attributes...]></[tagname]>");

$html

<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>

Syntax: unwrap

$doc->query("[selection]")->unwrap();

$html

<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">

Example test code snippet

Click here to go to example test.