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) { |
||||
0 ignored issues
–
show
|
|||||
33 | $text = qp($item)->text(); |
||||
34 | return strpos($text, 'Release') !== FALSE; |
||||
0 ignored issues
–
show
It seems like
$text can also be of type QueryPath\DOMQuery ; however, parameter $haystack of strpos() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
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 | ?> |
||||
0 ignored issues
–
show
It is not recommended to use PHP's closing tag
?> in files other than templates.
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore. A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever. ![]() |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.