Skip to content

Commit 6721d22

Browse files
Add files via upload
1 parent 2154c8f commit 6721d22

7 files changed

+133
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
include_once "../src/webparser.php";
3+
4+
// initialization
5+
$doc = new WebParser();
6+
$doc->loadHTML('
7+
<h1></h1>
8+
<form action="#" method="get">
9+
<div>
10+
<label for="name">Type your name:</label>
11+
<input type="text" name="name" id="age" value="" tabindex="1" />
12+
</div>
13+
</form>
14+
<button>submit</button>
15+
');
16+
//-----------------------------------------------------------------------------//
17+
18+
19+
$doc->query("h1")->editText("Not working form");
20+
$doc->output();
21+
22+
echo "<br>";
23+
24+
// if you are changing the same object you can write all its modifications in one line
25+
$doc->query("form input[name='name']")->addAttr("id", "name")->removeAttr("tabindex");
26+
$doc->query("button")->addAttr("onclick", "document.querySelector('form').submit()");
27+
28+
$doc->query("h1")->editText("Working form");
29+
$doc->output();

example_manipulating_images.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
include_once "../src/webparser.php";
3+
// initialization
4+
$doc = new WebParser();
5+
$doc->loadHTMLFile('https://en.wikipedia.org/wiki/COVID-19_pandemic');
6+
7+
$doc->query("img")->src("sick-sponge-bob.jpg");
8+
9+
$doc->output();

example_plain_text.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
include_once "../src/webparser.php";
3+
// initialization
4+
$doc = new WebParser();
5+
$doc->loadHTMLFile('https://en.wikipedia.org/wiki/COVID-19_pandemic');
6+
// * stands for "everything"
7+
echo $doc->query("*")->getText();

example_text_manipulation.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
include_once "../src/webparser.php";
3+
// initialization
4+
$doc = new WebParser();
5+
$doc->loadHTMLFile('https://en.wikipedia.org/wiki/COVID-19_pandemic');
6+
7+
// *::text stands for "text (::text) nodes inside all tags (*)"
8+
// replaces all a's with Z's
9+
$doc->query("*::text")->replaceText("/a/", "Z");
10+
11+
$doc->output();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
include_once "../src/webparser.php";
3+
// initialization
4+
$doc = new WebParser();
5+
$doc->loadHTMLFile('https://en.wikipedia.org/wiki/COVID-19_pandemic');
6+
7+
// a simple regex to match sentences
8+
$pattern = "/([A-Z][^\.!?]*[\.!?]*(<br>)*)/";
9+
// * stands for all tags
10+
// ::text queries node texts
11+
$doc->query("*::text")->replaceTextCallback($pattern, function($m){
12+
static $id = 0;
13+
$id++;
14+
15+
return "<label id='$id'>$m[1]</label>";
16+
}, true);
17+
// try changing true to false
18+
19+
$doc->output();

example_wrap_unwrap.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
include_once "../src/webparser.php";
3+
// initialization
4+
$doc = new WebParser();
5+
$doc->loadHTML('
6+
<div>
7+
<ul>
8+
<li><a href="#nowhere" title="Lorum ipsum dolor sit amet">Lorem</a></li>
9+
<li><a href="#nowhere" title="Aliquam tincidunt mauris eu risus">Aliquam</a></li>
10+
<li><a href="#nowhere" title="Morbi in sem quis dui placerat ornare">Morbi</a></li>
11+
<li><a href="#nowhere" title="Praesent dapibus, neque id cursus faucibus">Praesent</a></li>
12+
<li><a href="#nowhere" title="Pellentesque fermentum dolor">Pellentesque</a></li>
13+
</ul>
14+
</div>
15+
');
16+
17+
// queries all divs and unwraps them
18+
$doc->query("div ul")->unwrap();
19+
$doc->query("ul")->wrap('<section class="bookshelf"></section>');
20+
21+
// shows output
22+
$doc->output();
23+
// ----------------------------------------------------------------------------------------------------------
24+
$doc->loadHTML('
25+
<table class="data">
26+
<tr>
27+
<th>Entry Header 1</th>
28+
<th>Entry Header 2</th>
29+
<th>Entry Header 3</th>
30+
<th>Entry Header 4</th>
31+
</tr>
32+
<tr>
33+
<td>Entry First Line 1</td>
34+
<td>Entry First Line 2</td>
35+
<td>Entry First Line 3</td>
36+
<td>Entry First Line 4</td>
37+
</tr>
38+
<tr>
39+
<td>Entry Line 1</td>
40+
<td>Entry Line 2</td>
41+
<td>Entry Line 3</td>
42+
<td>Entry Line 4</td>
43+
</tr>
44+
<tr>
45+
<td>Entry Last Line 1</td>
46+
<td>Entry Last Line 2</td>
47+
<td>Entry Last Line 3</td>
48+
<td>Entry Last Line 4</td>
49+
</tr>
50+
</table>
51+
');
52+
53+
$doc->query("table.data tr td[1]::text")->wrap('<span style="color: red; font-weight: bold;"></span>');
54+
$doc->query("table.data tr td[2]::text")->wrap('<span style="color: blue; font-weight: bold;"></span>');
55+
$doc->query("table.data tr td[3]::text")->wrap('<span style="color: green; font-weight: bold;"></span>');
56+
$doc->query("table.data tr td[4]::text")->wrap('<span style="color: orange; font-weight: bold;"></span>');
57+
58+
$doc->output();

sick-sponge-bob.jpg

45.6 KB
Loading

0 commit comments

Comments
 (0)