Skip to content

Commit c1dcfa7

Browse files
committed
Continue refactoring examples
1 parent ee2e99f commit c1dcfa7

File tree

9 files changed

+211
-209
lines changed

9 files changed

+211
-209
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/** @file
3+
* Using QueryPath.
4+
*
5+
* This file contains an example of how QueryPath can be used
6+
* to generate web pages. Part of the design of this example is to exhibit many
7+
* different QueryPath functions in one long chain. All of the methods shown
8+
* here are fully documented in {@link \QueryPath\QueryPath}.
9+
*
10+
* The method used in this example is a typical example of how QueryPath can
11+
* gradually build up content.
12+
*
13+
* @author M Butcher <[email protected]>
14+
* @license LGPL The GNU Lesser GPL (LGPL) or an MIT-like license.
15+
*/
16+
17+
require_once __DIR__ . '/../../vendor/autoload.php';
18+
19+
try {
20+
// Begin with an HTML5 stub document and navigate to the title.
21+
html5qp(\QueryPath\QueryPath::HTML5_STUB, 'title')
22+
// Add some text to the title
23+
->text('Example of QueryPath.')
24+
// Now look for the <body> element
25+
->top('body')
26+
// Inside the body, add a title and paragraph.
27+
->append('<h1>This is a test page</h1><p>Test text</p>')
28+
// Now we select the paragraph we just created inside the body
29+
->children('p')
30+
// Add a 'class="some-class"' attribute to the paragraph
31+
->attr('class', 'some-class')
32+
// And add a style attribute, too, setting the background color.
33+
->css('background-color', '#eee')
34+
// Now go back to the paragraph again
35+
->parent()
36+
// Before the paragraph and the title, add an empty table.
37+
->prepend('<table id="my-table"></table>')
38+
// Now let's go to the table...
39+
->top('#my-table')
40+
// Add a couple of empty rows
41+
->append('<tr></tr><tr></tr>')
42+
// select the rows (both at once)
43+
->children()
44+
// Add a CSS class to both rows
45+
->addClass('table-row')
46+
// Now just get the first row (at position 0)
47+
->eq(0)
48+
// Add a table header in the first row
49+
->append('<th>This is the header</th>')
50+
// Now go to the next row
51+
->next()
52+
// Add some data to this row
53+
->append('<td>This is the data</td>')
54+
// Write it all out as HTML
55+
->writeHTML5();
56+
} catch (\QueryPath\Exception $e) {
57+
die($e->getMessage());
58+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Using QueryPath.
4+
*
5+
* This file contains an example of how QueryPath can be used
6+
* to generate XML.
7+
*
8+
* QueryPath's ability to handle arbitrary XML comes in handy. Fragments of HTML
9+
* can be composed as external XML documents, and then inserted selectively into
10+
* an HTML document as needed. Just remember: Every XML document (even just a
11+
* string) needs to begin with the XML declaration: `<?xml version="1.0"?>`
12+
*
13+
* @author M Butcher <[email protected]>
14+
* @license LGPL The GNU Lesser GPL (LGPL) or an MIT-like license.
15+
*/
16+
17+
require_once __DIR__ . '/../../vendor/autoload.php';
18+
19+
/*
20+
* Create a new XML document wrapped in a QueryPath.
21+
* By default, it will point to the root element `<author />`
22+
*/
23+
24+
try {
25+
echo qp('<?xml version="1.0"?><author></author>')
26+
// Add a new last name inside of author.
27+
->append('<lastName>Wiseman</lastName>')
28+
// Select all of the children of <author/>. In this case,
29+
// that is <lastName/>
30+
->children()
31+
// Oh, wait... we wanted last name to be inside of a <name/>
32+
// element. Use wrap to wrap the current element in something:
33+
->wrap('<name/>')
34+
// And before last name, we want to add first name.
35+
->before('<firstName/>')
36+
// Select first name
37+
->prev()
38+
// Set the text of first name
39+
->text('Simon')
40+
// And then after first name, add the patronymic
41+
->after('<middleName>J.</middleName>')
42+
// Now go back to the root element, the top of the document.
43+
->top()
44+
// Add another tag -- origin.
45+
->append('<origin>Australia</origin>')
46+
// turn the QueryPath contents back into a string. Since we are
47+
// at the top of the document, the whole document will be converted
48+
// to a string.
49+
->xml();
50+
} catch (\QueryPath\Exception $e) {
51+
die($e->getMessage());
52+
}

examples/fetch_rss.php

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Filtering by Text Content.
4+
*
5+
* This example shows how to filter and match HTML by its content
6+
* The `:contains()` pseudo-class performs a substring match, and it
7+
* a simple way to achieve this.
8+
*
9+
* For more powerful filtering, you can pass your own callback to `filterCallback()`
10+
* to determine if an item should be kept in the QueryPath matches, so it can be
11+
* manipulated further, or excluded.
12+
*
13+
* @author M Butcher <[email protected]>
14+
* @license LGPL The GNU Lesser GPL (LGPL) or an MIT-like license.
15+
*/
16+
17+
require_once __DIR__ . '/../../vendor/autoload.php';
18+
19+
try {
20+
$qp = html5qp('https://www.php.net');
21+
22+
echo '<h1>Filtering Content</h1>';
23+
echo '<h2>PHP Releases</h2>';
24+
25+
/* Get any posts containing the word 'Release' */
26+
echo $qp->find('h2.newstitle a:contains(Release)')
27+
->textImplode('<br>' . PHP_EOL);
28+
29+
echo '<h2>PHP news in the past 30 days...</h2>';
30+
31+
echo $qp->find('header.title')
32+
->filterCallback(function ($index, $item) {
33+
/*
34+
* Returns TRUE to keep current $item in matches, or FALSE to remove
35+
*
36+
* $item is a DOMNode (actually, a DOMElement). So if we wanted to do QueryPath
37+
* manipulations on it, you can pass it to html5qp()
38+
*/
39+
40+
/* Get the current post datetime */
41+
$datetime = new DateTimeImmutable(html5qp($item, 'time')->attr('datetime'));
42+
43+
/* Keep any posts less than 30 days old */
44+
return $datetime > (new DateTimeImmutable('-30 days'));
45+
})
46+
->find('a')
47+
->textImplode('<br>' . PHP_EOL);
48+
} catch (\QueryPath\Exception $e) {
49+
die($e->getMessage());
50+
}

examples/html.php

Lines changed: 0 additions & 56 deletions
This file was deleted.

examples/matching_text_content.php

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Retrieving remote RSS feeds.
4+
*
5+
* This file contains an example of how QueryPath can be used
6+
* to retrieve and parse a remote RSS feed. If PHP is configured to allow
7+
* HTTP URLs for remote hosts in file manipulation functions, you can use
8+
* QueryPath to retrieve the remote file and parse it.
9+
*
10+
* In this example, we grab the RSS feed from remote server and
11+
* parse it. From there, we make a list of hyperlinks, one for each item in
12+
* the original feed.
13+
*
14+
* @author M Butcher <[email protected]>
15+
* @license LGPL The GNU Lesser GPL (LGPL) or an MIT-like license.
16+
*/
17+
require_once __DIR__ . '/../../vendor/autoload.php';
18+
19+
// The URL of the remote RSS feed.
20+
$remote = 'https://en.wikipedia.org/w/index.php?title=Special:NewPages&feed=rss';
21+
22+
try {
23+
// We will write the results into this document.
24+
$qp = html5qp(\QueryPath\QueryPath::HTML5_STUB, 'title')
25+
->text('New Wikipedia Pages')
26+
->top('body')
27+
->append('<h1>New Wikipedia Pages</h1>')
28+
->append('<ul/>')
29+
->children('ul');
30+
31+
// Load the remote document and loop through all the items.
32+
foreach (qp($remote, 'channel>item') as $item) {
33+
// Get title and link.
34+
$title = $item->find('title')->text();
35+
$link = $item->find('link')->text();
36+
37+
$list = html5qp('<li/>', 'li')
38+
->append('<a/>')
39+
->find('a')
40+
->attr('href', $link)
41+
->text($title);
42+
43+
// Add it to the output document.
44+
$qp->append($list->top()->innerHTML5());
45+
}
46+
47+
// Write the results.
48+
$qp->writeHTML5();
49+
} catch (Exception $e) {
50+
die($e->getMessage());
51+
}

examples/remote-filter-and-retrieval/index.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
* @author Emily Brand
66
* @license LGPL The GNU Lesser GPL (LGPL) or an MIT-like license.
77
*
8-
* @internal IMPORTANT: if you don't trust the source of the data being loaded make sure to sanitize the output
9-
*
108
* @see https://www.urbandictionary.com/
11-
* @see https://github.com/symfony/html-sanitizer
129
*/
1310
require_once __DIR__ . '/../../vendor/autoload.php';
1411

0 commit comments

Comments
 (0)