1
|
|
|
<?php
|
|
|
|
|
2
|
|
|
/**
|
3
|
|
|
* @author Niels A.D.
|
4
|
|
|
* @author Todd Burry <[email protected]>
|
5
|
|
|
* @copyright 2010 Niels A.D., 2014 Todd Burry
|
6
|
|
|
* @license http://opensource.org/licenses/LGPL-2.1 LGPL-2.1
|
7
|
|
|
* @package pQuery
|
8
|
|
|
*/
|
9
|
|
|
|
10
|
|
|
use pQuery\Html5Parser;
|
11
|
|
|
use pQuery\HtmlFormatter;
|
12
|
|
|
|
13
|
|
|
/**
|
14
|
|
|
* Returns HTML DOM from string
|
15
|
|
|
* @param string $str
|
16
|
|
|
* @param bool $return_root Return root node or return parser object
|
17
|
|
|
* @return Html5Parser|DomNode
|
18
|
|
|
*/
|
19
|
|
|
function str_get_dom($str, $return_root = true) {
|
20
|
|
|
$a = new Html5Parser($str);
|
21
|
|
|
return (($return_root) ? $a->root : $a);
|
|
|
|
|
22
|
|
|
}
|
23
|
|
|
|
24
|
|
|
/**
|
25
|
|
|
* Returns HTML DOM from file/website
|
26
|
|
|
* @param string $str
|
|
|
|
|
27
|
|
|
* @param bool $return_root Return root node or return parser object
|
28
|
|
|
* @param bool $use_include_path Use include path search in file_get_contents
|
29
|
|
|
* @param resource $context Context resource used in file_get_contents (PHP >= 5.0.0)
|
30
|
|
|
* @return Html5Parser|DomNode
|
31
|
|
|
*/
|
32
|
|
|
function file_get_dom($file, $return_root = true, $use_include_path = false, $context = null) {
|
33
|
|
|
if (version_compare(PHP_VERSION, '5.0.0', '>='))
|
34
|
|
|
$f = file_get_contents($file, $use_include_path, $context);
|
35
|
|
|
else {
|
36
|
|
|
if ($context !== null)
|
37
|
|
|
trigger_error('Context parameter not supported in this PHP version');
|
38
|
|
|
$f = file_get_contents($file, $use_include_path);
|
39
|
|
|
}
|
40
|
|
|
|
41
|
|
|
return (($f === false) ? false : str_get_dom($f, $return_root));
|
42
|
|
|
}
|
43
|
|
|
|
44
|
|
|
/**
|
45
|
|
|
* Format/beautify DOM
|
46
|
|
|
* @param DomNode $root
|
47
|
|
|
* @param array $options Extra formatting options {@link Formatter::$options}
|
48
|
|
|
* @return bool
|
49
|
|
|
*/
|
50
|
|
|
function dom_format(&$root, $options = array()) {
|
51
|
|
|
$formatter = new HtmlFormatter($options);
|
52
|
|
|
return $formatter->format($root);
|
53
|
|
|
}
|
54
|
|
|
|
55
|
|
|
if (version_compare(PHP_VERSION, '5.0.0', '<')) {
|
56
|
|
|
/**
|
57
|
|
|
* PHP alternative to str_split, for backwards compatibility
|
58
|
|
|
* @param string $string
|
59
|
|
|
* @return string
|
60
|
|
|
*/
|
61
|
|
|
function str_split($string) {
|
62
|
|
|
$res = array();
|
63
|
|
|
$size = strlen($string);
|
64
|
|
|
for ($i = 0; $i < $size; $i++) {
|
65
|
|
|
$res[] = $string[$i];
|
66
|
|
|
}
|
67
|
|
|
|
68
|
|
|
return $res;
|
69
|
|
|
}
|
70
|
|
|
}
|
71
|
|
|
|
72
|
|
|
if (version_compare(PHP_VERSION, '5.2.0', '<')) {
|
73
|
|
|
/**
|
74
|
|
|
* PHP alternative to array_fill_keys, for backwards compatibility
|
75
|
|
|
* @param array $keys
|
76
|
|
|
* @param mixed $value
|
77
|
|
|
* @return array
|
78
|
|
|
*/
|
79
|
|
|
function array_fill_keys($keys, $value) {
|
80
|
|
|
$res = array();
|
81
|
|
|
foreach($keys as $k) {
|
82
|
|
|
$res[$k] = $value;
|
83
|
|
|
}
|
84
|
|
|
|
85
|
|
|
return $res;
|
86
|
|
|
}
|
87
|
|
|
}
|
88
|
|
|
|
89
|
|
|
#!! <- Ignore when converting to single file
|
90
|
|
|
if (!defined('GANON_NO_INCLUDES')) {
|
91
|
|
|
define('GANON_NO_INCLUDES', true);
|
92
|
|
|
include_once('IQuery.php');
|
93
|
|
|
include_once('gan_tokenizer.php');
|
94
|
|
|
include_once('gan_parser_html.php');
|
95
|
|
|
include_once('gan_node_html.php');
|
96
|
|
|
include_once('gan_selector_html.php');
|
97
|
|
|
include_once('gan_formatter.php');
|
98
|
|
|
}
|
99
|
|
|
#!
|
100
|
|
|
|
101
|
|
|
?> |
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.