|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Matching Text Content. |
|
4
|
|
|
* |
|
5
|
|
|
* This example shows one way of matching text content. |
|
6
|
|
|
* The `:contains()` pseudo-class requires that the ENTIRE CONTENTS |
|
7
|
|
|
* of an element match exactly. But sometimes what we want is a way |
|
8
|
|
|
* to match just part of the contents of an element. This example |
|
9
|
|
|
* illustrates how to accomplish this with a filter callback. |
|
10
|
|
|
* |
|
11
|
|
|
* As of QueryPath 2.1beta2, `:contains()` performs a substring match instead of |
|
12
|
|
|
* and exact match, so the method outline below is roughly the same as merely |
|
13
|
|
|
* using `:contains(Release)`. |
|
14
|
|
|
* |
|
15
|
|
|
* |
|
16
|
|
|
* @author M Butcher <[email protected]> |
|
17
|
|
|
* @license LGPL The GNU Lesser GPL (LGPL) or an MIT-like license. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
/** Include QueryPath. */ |
|
21
|
|
|
require_once '../src/QueryPath/QueryPath.php'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Check if the string 'Release' is in the text content of any matched nodes. |
|
25
|
|
|
* |
|
26
|
|
|
* Returns TRUE if the text is found, FALSE otherwise. Anytime a filter callback |
|
27
|
|
|
* returns FALSE, QueryPath will remove it from the matches. |
|
28
|
|
|
* |
|
29
|
|
|
* Note that $item is a DOMNode (actually, a DOMElement). So if we wanted to do QueryPath |
|
30
|
|
|
* manipulations on it, we could wrap it in a `qp()`. |
|
31
|
|
|
*/ |
|
32
|
|
|
function exampleCallback($index, $item) { |
|
|
|
|
|
|
33
|
|
|
$text = qp($item)->text(); |
|
34
|
|
|
return strpos($text, 'Release') !== FALSE; |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/* |
|
38
|
|
|
* This is the QueryPath call. |
|
39
|
|
|
* |
|
40
|
|
|
* First we fetch the remote page, parse it, and grab just the `a` tags inside of the summary. |
|
41
|
|
|
* Then we filter the results through our callback. |
|
42
|
|
|
* Finally, we fetch all of the matching text and print it. |
|
43
|
|
|
* |
|
44
|
|
|
* NOTE: If you are using PHP 5.3, you can define the callback inline instead of separating it |
|
45
|
|
|
* into a stand-alone function. |
|
46
|
|
|
*/ |
|
47
|
|
|
print htmlqp('http://php.net/', 'h1.summary a') |
|
48
|
|
|
->filterCallback('exampleCallback') |
|
49
|
|
|
->textImplode(PHP_EOL); |
|
50
|
|
|
?> |
|
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.