| 3 |
liveuser |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
|
|
|
5 |
QBPWCF, Quick Build PHP website Component base on Fedora Linux.
|
|
|
6 |
Copyright (C) 2015~2024 Min-Jhin,Chen
|
|
|
7 |
|
|
|
8 |
This file is part of QBPWCF.
|
|
|
9 |
|
|
|
10 |
QBPWCF is free software: you can redistribute it and/or modify
|
|
|
11 |
it under the terms of the GNU General Public License as published by
|
|
|
12 |
the Free Software Foundation, either version 3 of the License, or
|
|
|
13 |
(at your option) any later version.
|
|
|
14 |
|
|
|
15 |
QBPWCF is distributed in the hope that it will be useful,
|
|
|
16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
18 |
GNU General Public License for more details.
|
|
|
19 |
|
|
|
20 |
You should have received a copy of the GNU General Public License
|
|
|
21 |
along with QBPWCF. If not, see <http://www.gnu.org/licenses/>.
|
|
|
22 |
|
|
|
23 |
*/
|
|
|
24 |
namespace qbpwcf;
|
|
|
25 |
|
|
|
26 |
/*
|
|
|
27 |
類別說明:
|
|
|
28 |
提供html處理的類別.
|
|
|
29 |
備註:
|
|
|
30 |
無.
|
|
|
31 |
*/
|
|
|
32 |
class html{
|
|
|
33 |
|
|
|
34 |
/*
|
|
|
35 |
#函式說明:
|
|
|
36 |
#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
|
|
|
37 |
#回傳結果:
|
|
|
38 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
39 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
40 |
#$result["function"],當前執行的函式名稱.
|
|
|
41 |
#必填參數:
|
|
|
42 |
#$method,物件,為物件實體或類別名稱,會自動置入該參數.
|
|
|
43 |
#$arguments,陣列,為呼叫方法時所用的參數.
|
|
|
44 |
#參考資料:
|
|
|
45 |
#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
|
|
|
46 |
*/
|
|
|
47 |
public function __call($method,$arguments){
|
|
|
48 |
|
|
|
49 |
#取得當前執行的函式
|
|
|
50 |
$result["function"]=__FUNCTION__;
|
|
|
51 |
|
|
|
52 |
#設置執行不正常
|
|
|
53 |
$result["status"]="false";
|
|
|
54 |
|
|
|
55 |
#設置執行錯誤
|
|
|
56 |
$result["error"][]=__NAMESPACE__ ."/".$method."() 不存在!";
|
|
|
57 |
|
|
|
58 |
#設置所丟入的參數
|
|
|
59 |
$result["error"][]=$arguments;
|
|
|
60 |
|
|
|
61 |
#回傳結果
|
|
|
62 |
return $result;
|
|
|
63 |
|
|
|
64 |
}#function __call end
|
|
|
65 |
|
|
|
66 |
/*
|
|
|
67 |
#函式說明:
|
|
|
68 |
#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
|
|
|
69 |
#回傳結果:
|
|
|
70 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
71 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
72 |
#$result["function"],當前執行的函式名稱.
|
|
|
73 |
#必填參數:
|
|
|
74 |
#$method,物件,為物件實體或類別名稱,會自動置入該參數.
|
|
|
75 |
#$arguments,陣列,為呼叫方法時所用的參數.
|
|
|
76 |
#參考資料:
|
|
|
77 |
#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
|
|
|
78 |
*/
|
|
|
79 |
public static function __callStatic($method,$arguments){
|
|
|
80 |
|
|
|
81 |
#取得當前執行的函式
|
|
|
82 |
$result["function"]=__FUNCTION__;
|
|
|
83 |
|
|
|
84 |
#設置執行不正常
|
|
|
85 |
$result["status"]="false";
|
|
|
86 |
|
|
|
87 |
#設置執行錯誤
|
|
|
88 |
$result["error"][]="欲呼叫的". __NAMESPACE__ ."/".$method."() 不存在!";
|
|
|
89 |
|
|
|
90 |
#設置所丟入的參數
|
|
|
91 |
$result["error"][]=$arguments;
|
|
|
92 |
|
|
|
93 |
#回傳結果
|
|
|
94 |
return $result;
|
|
|
95 |
|
|
|
96 |
}#function __callStatic end
|
|
|
97 |
|
|
|
98 |
/*
|
|
|
99 |
#涵式說明:
|
|
|
100 |
#解析html字串
|
|
|
101 |
#回傳結果:
|
|
|
102 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
103 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
104 |
#$result["function"],當前執行的函式名稱.
|
|
|
105 |
#$result["founded"],是否有抓到html檔案,"true"代表有抓到,"false"代表沒有抓到.
|
|
|
106 |
#$result["content"],html的物件內容.
|
|
|
107 |
#$result["argu"],所使用的參數.
|
|
|
108 |
#必填參數:
|
|
|
109 |
#$conf["htmlStr"],字串,xml檔案的位置.
|
|
|
110 |
$conf["htmlStr"]="";
|
|
|
111 |
#參考資料來源:
|
|
|
112 |
#讀取html字串=>http://php.net/manual/en/domdocument.loadhtml.php
|
|
|
113 |
#讀取tag已知名稱的屬性=>http://php.net/manual/en/domelement.hasattribute.php
|
|
|
114 |
#讀取tag的所有屬性=>http://stackoverflow.com/questions/2385834/php-dom-get-all-attributes-of-a-node
|
|
|
115 |
#load utf8 html=>http://stackoverflow.com/questions/8218230/php-domdocument-loadhtml-not-encoding-utf-8-correctly
|
|
|
116 |
*/
|
|
|
117 |
function parseHtmlStr(&$conf){
|
|
|
118 |
|
|
|
119 |
#初始化要回傳的結果
|
|
|
120 |
$result=array();
|
|
|
121 |
|
|
|
122 |
#設置當其函數名稱
|
|
|
123 |
$result["function"]=__FUNCTION__;
|
|
|
124 |
|
|
|
125 |
#如果 $conf 不為陣列
|
|
|
126 |
if(gettype($conf)!="array"){
|
|
|
127 |
|
|
|
128 |
#設置執行失敗
|
|
|
129 |
$result["status"]="false";
|
|
|
130 |
|
|
|
131 |
#設置執行錯誤訊息
|
|
|
132 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
133 |
|
|
|
134 |
#如果傳入的參數為 null
|
|
|
135 |
if($conf==null){
|
|
|
136 |
|
|
|
137 |
#設置執行錯誤訊息
|
|
|
138 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
|
|
139 |
|
|
|
140 |
}#if end
|
|
|
141 |
|
|
|
142 |
#回傳結果
|
|
|
143 |
return $result;
|
|
|
144 |
|
|
|
145 |
}#if end
|
|
|
146 |
|
|
|
147 |
#取得參數
|
|
|
148 |
$result["argu"]=$conf;
|
|
|
149 |
|
|
|
150 |
#檢查參數
|
|
|
151 |
#函式說明:
|
|
|
152 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
153 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
154 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
155 |
#$result["function"],當前執行的函式名稱.
|
|
|
156 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
157 |
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
158 |
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
159 |
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
160 |
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
|
|
|
161 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
162 |
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
|
|
|
163 |
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
|
|
|
164 |
#必填寫的參數:
|
|
|
165 |
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
166 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
167 |
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
168 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("htmlStr");
|
|
|
169 |
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object");
|
|
|
170 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
|
|
|
171 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
172 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
173 |
#可以省略的參數:
|
|
|
174 |
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
|
|
|
175 |
#$conf["canBeEmptyString"]="false";
|
|
|
176 |
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或集合.
|
|
|
177 |
#$conf["skipableVariableCanNotBeEmpty"]=array();
|
|
|
178 |
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
179 |
#$conf["skipableVariableName"]=array();
|
|
|
180 |
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
|
|
181 |
#$conf["skipableVariableType"]=array();
|
|
|
182 |
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
183 |
#$conf["skipableVarDefaultValue"]=array("");
|
|
|
184 |
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
185 |
#$conf["arrayCountEqualCheck"][]=array();
|
|
|
186 |
#參考資料來源:
|
|
|
187 |
#array_keys=>http://php.net/manual/en/function.array-keys.php
|
|
|
188 |
$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
189 |
unset($conf["variableCheck::checkArguments"]);
|
|
|
190 |
|
|
|
191 |
#如果檢查失敗
|
|
|
192 |
if($checkArguments["status"]=="false"){
|
|
|
193 |
|
|
|
194 |
#設置錯誤識別
|
|
|
195 |
$result["status"]="false";
|
|
|
196 |
|
|
|
197 |
#設置錯誤訊息
|
|
|
198 |
$result["error"]=$checkArguments;
|
|
|
199 |
|
|
|
200 |
#回傳結果
|
|
|
201 |
return $result;
|
|
|
202 |
|
|
|
203 |
}#if end
|
|
|
204 |
|
|
|
205 |
#如果檢查不通過
|
|
|
206 |
if($checkArguments["passed"]=="false"){
|
|
|
207 |
|
|
|
208 |
#設置錯誤識別
|
|
|
209 |
$result["status"]="false";
|
|
|
210 |
|
|
|
211 |
#設置錯誤訊息
|
|
|
212 |
$result["error"]=$checkArguments;
|
|
|
213 |
|
|
|
214 |
#回傳結果
|
|
|
215 |
return $result;
|
|
|
216 |
|
|
|
217 |
}#if end
|
|
|
218 |
|
|
|
219 |
#實做類別 DOMDocument
|
|
|
220 |
$domd = new \DOMDocument();
|
|
|
221 |
|
|
|
222 |
#載入html字串
|
|
|
223 |
$domd->loadHTML($conf["htmlStr"]);
|
|
|
224 |
|
|
|
225 |
#正規化html字串
|
|
|
226 |
$domd->normalizeDocument();
|
|
|
227 |
|
|
|
228 |
#取得所有節點的內容
|
|
|
229 |
$allTags=$domd->getElementsByTagName('*');
|
|
|
230 |
|
|
|
231 |
#有幾個標籤就執行幾次
|
|
|
232 |
foreach($allTags as $node){
|
|
|
233 |
|
|
|
234 |
#紀錄節點的路徑
|
|
|
235 |
$nodePath=$node->getNodePath();
|
|
|
236 |
|
|
|
237 |
#$nodePath內容範例:
|
|
|
238 |
/*
|
|
|
239 |
/html/head
|
|
|
240 |
/html/head/meta
|
|
|
241 |
/html/body
|
|
|
242 |
/html/body/div[1]
|
|
|
243 |
/html/body/div[2]
|
|
|
244 |
*/
|
|
|
245 |
|
|
|
246 |
#初始化暫存屬性的陣列
|
|
|
247 |
$nodeAttr=array();
|
|
|
248 |
|
|
|
249 |
#如果有屬性存在
|
|
|
250 |
if($node->hasAttributes()){
|
|
|
251 |
|
|
|
252 |
#有幾個屬性就執行幾次
|
|
|
253 |
foreach ($node->attributes as $attr){
|
|
|
254 |
|
|
|
255 |
#紀錄屬性與值
|
|
|
256 |
$nodeAttr[$attr->nodeName]=$attr->nodeValue;
|
|
|
257 |
|
|
|
258 |
}#$foreach end
|
|
|
259 |
|
|
|
260 |
}#if end
|
|
|
261 |
|
|
|
262 |
#紀錄domd物件
|
|
|
263 |
$result["tree"][$nodePath]["domd"]=$node;
|
|
|
264 |
|
|
|
265 |
#紀錄文字內容
|
|
|
266 |
$result["tree"][$nodePath]["textContent"]=$node->textContent;
|
|
|
267 |
|
|
|
268 |
#紀錄屬性
|
|
|
269 |
$result["tree"][$nodePath]["attr"]=$nodeAttr;
|
|
|
270 |
|
|
|
271 |
}#foreach end
|
|
|
272 |
|
|
|
273 |
#設置 DOMDocument 物件
|
|
|
274 |
$result["domd"]=$domd;
|
|
|
275 |
|
|
|
276 |
#設置執行正常
|
|
|
277 |
$result["status"]="true";
|
|
|
278 |
|
|
|
279 |
#回傳抓取到的解果
|
|
|
280 |
return $result;
|
|
|
281 |
|
|
|
282 |
}#function parseHtmlStr end
|
|
|
283 |
|
|
|
284 |
}#class html end
|
|
|
285 |
|
|
|
286 |
?>
|