Subversion Repositories php-qbpwcf

Rev

Rev 3 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3 Rev 226
Line 1... Line 1...
1
<?php
1
<?php
2
 
2
 
3
/*
3
/*
4
 
4
 
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
6
    Copyright (C) 2015~2024 Min-Jhin,Chen
6
    Copyright (C) 2014~2025 MIN ZHI, CHEN
7
 
7
 
8
    This file is part of QBPWCF.
8
    This file is part of QBPWCF.
9
 
9
 
10
    QBPWCF is free software: you can redistribute it and/or modify
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
11
    it under the terms of the GNU General Public License as published by
Line 43... Line 43...
43
	#$arguments,陣列,為呼叫方法時所用的參數.
43
	#$arguments,陣列,為呼叫方法時所用的參數.
44
	#參考資料:
44
	#參考資料:
45
	#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
45
	#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
46
	*/
46
	*/
47
	public function __call($method,$arguments){
47
	public function __call($method,$arguments){
48
		
48
 
49
		#取得當前執行的函式
49
		#取得當前執行的函式
50
		$result["function"]=__FUNCTION__;
50
		$result["function"]=__FUNCTION__;
51
		
51
 
52
		#設置執行不正常
52
		#設置執行不正常
53
		$result["status"]="false";
53
		$result["status"]="false";
54
		
54
 
55
		#設置執行錯誤
55
		#設置執行錯誤
56
		$result["error"][]=__NAMESPACE__ ."/".$method."() 不存在!";
56
		$result["error"][]=__NAMESPACE__ ."/".$method."() 不存在!";
57
		
57
 
58
		#設置所丟入的參數
58
		#設置所丟入的參數
59
		$result["error"][]=$arguments;
59
		$result["error"][]=$arguments;
60
		
60
 
61
		#回傳結果
61
		#回傳結果
62
		return $result;
62
		return $result;
63
		
63
 
64
		}#function __call end
64
		}#function __call end
65
		
65
 
66
	/*
66
	/*
67
	#函式說明:
67
	#函式說明:
68
	#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
68
	#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
69
	#回傳結果:
69
	#回傳結果:
70
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
70
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 75... Line 75...
75
	#$arguments,陣列,為呼叫方法時所用的參數.
75
	#$arguments,陣列,為呼叫方法時所用的參數.
76
	#參考資料:
76
	#參考資料:
77
	#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
77
	#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
78
	*/
78
	*/
79
	public static function __callStatic($method,$arguments){
79
	public static function __callStatic($method,$arguments){
80
		
80
 
81
		#取得當前執行的函式
81
		#取得當前執行的函式
82
		$result["function"]=__FUNCTION__;
82
		$result["function"]=__FUNCTION__;
83
		
83
 
84
		#設置執行不正常
84
		#設置執行不正常
85
		$result["status"]="false";
85
		$result["status"]="false";
86
		
86
 
87
		#設置執行錯誤
87
		#設置執行錯誤
88
		$result["error"][]="欲呼叫的". __NAMESPACE__ ."/".$method."() 不存在!";
88
		$result["error"][]="欲呼叫的". __NAMESPACE__ ."/".$method."() 不存在!";
89
		
89
 
90
		#設置所丟入的參數
90
		#設置所丟入的參數
91
		$result["error"][]=$arguments;
91
		$result["error"][]=$arguments;
92
		
92
 
93
		#回傳結果
93
		#回傳結果
94
		return $result;
94
		return $result;
95
		
95
 
96
		}#function __callStatic end
96
		}#function __callStatic end
97
 
97
 
98
	/*
98
	/*
99
	#涵式說明:
99
	#涵式說明:
100
	#解析html字串
100
	#解析html字串
Line 113... Line 113...
113
	#讀取tag已知名稱的屬性=>http://php.net/manual/en/domelement.hasattribute.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
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
115
	#load utf8 html=>http://stackoverflow.com/questions/8218230/php-domdocument-loadhtml-not-encoding-utf-8-correctly
116
	*/
116
	*/
117
	function parseHtmlStr(&$conf){
117
	function parseHtmlStr(&$conf){
118
		
118
 
119
		#初始化要回傳的結果
119
		#初始化要回傳的結果
120
		$result=array();
120
		$result=array();
121
		
121
 
122
		#設置當其函數名稱
122
		#設置當其函數名稱
123
		$result["function"]=__FUNCTION__;
123
		$result["function"]=__FUNCTION__;
124
		
124
 
125
		#如果 $conf 不為陣列
125
		#如果 $conf 不為陣列
126
		if(gettype($conf)!="array"){
126
		if(gettype($conf)!="array"){
127
			
127
 
128
			#設置執行失敗
128
			#設置執行失敗
129
			$result["status"]="false";
129
			$result["status"]="false";
130
			
130
 
131
			#設置執行錯誤訊息
131
			#設置執行錯誤訊息
132
			$result["error"][]="\$conf變數須為陣列形態";
132
			$result["error"][]="\$conf變數須為陣列形態";
133
 
133
 
134
			#如果傳入的參數為 null
134
			#如果傳入的參數為 null
135
			if($conf==null){
135
			if($conf==null){
136
				
136
 
137
				#設置執行錯誤訊息
137
				#設置執行錯誤訊息
138
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
138
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
139
				
139
 
140
				}#if end
140
				}#if end
141
 
141
 
142
			#回傳結果
142
			#回傳結果
143
			return $result;
143
			return $result;
144
			
144
 
145
			}#if end
145
			}#if end
146
		
146
 
147
		#取得參數
147
		#取得參數
148
		$result["argu"]=$conf;
148
		$result["argu"]=$conf;
149
		
149
 
150
		#檢查參數
150
		#檢查參數
151
		#函式說明:
151
		#函式說明:
152
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
152
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
153
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
153
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
154
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
154
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
Line 164... Line 164...
164
		#必填寫的參數:
164
		#必填寫的參數:
165
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
165
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
166
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
166
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
167
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
167
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
168
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("htmlStr");
168
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("htmlStr");
169
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); 
169
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object");
170
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
170
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
171
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
171
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
172
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
172
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
173
		#可以省略的參數:
173
		#可以省略的參數:
174
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
174
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
175
		#$conf["canBeEmptyString"]="false";
175
		#$conf["canBeEmptyString"]="false";
176
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或集合.
176
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或集合.
177
		#$conf["skipableVariableCanNotBeEmpty"]=array();
177
		#$conf["skipableVariableCanNotBeEmpty"]=array();
178
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
178
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
179
		#$conf["skipableVariableName"]=array();
179
		#$conf["skipableVariableName"]=array();
180
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double"); 
180
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
181
		#$conf["skipableVariableType"]=array();
181
		#$conf["skipableVariableType"]=array();
182
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
182
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
183
		#$conf["skipableVarDefaultValue"]=array("");
183
		#$conf["skipableVarDefaultValue"]=array("");
184
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
184
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
185
		#$conf["arrayCountEqualCheck"][]=array();
185
		#$conf["arrayCountEqualCheck"][]=array();
186
		#參考資料來源:
186
		#參考資料來源:
187
		#array_keys=>http://php.net/manual/en/function.array-keys.php
187
		#array_keys=>http://php.net/manual/en/function.array-keys.php
188
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
188
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
189
		unset($conf["variableCheck::checkArguments"]);
189
		unset($conf["variableCheck::checkArguments"]);
190
		
190
 
191
		#如果檢查失敗
191
		#如果檢查失敗
192
		if($checkArguments["status"]=="false"){
192
		if($checkArguments["status"]=="false"){
193
			
193
 
194
			#設置錯誤識別
194
			#設置錯誤識別
195
			$result["status"]="false";
195
			$result["status"]="false";
196
			
196
 
197
			#設置錯誤訊息
197
			#設置錯誤訊息
198
			$result["error"]=$checkArguments;
198
			$result["error"]=$checkArguments;
199
			
199
 
200
			#回傳結果
200
			#回傳結果
201
			return $result;
201
			return $result;
202
			
202
 
203
			}#if end
203
			}#if end
204
	
204
 
205
		#如果檢查不通過
205
		#如果檢查不通過
206
		if($checkArguments["passed"]=="false"){
206
		if($checkArguments["passed"]=="false"){
207
			
207
 
208
			#設置錯誤識別
208
			#設置錯誤識別
209
			$result["status"]="false";
209
			$result["status"]="false";
210
			
210
 
211
			#設置錯誤訊息
211
			#設置錯誤訊息
212
			$result["error"]=$checkArguments;
212
			$result["error"]=$checkArguments;
213
			
213
 
214
			#回傳結果
214
			#回傳結果
215
			return $result;
215
			return $result;
216
			
216
 
217
			}#if end
217
			}#if end
218
		
218
 
219
		#實做類別 DOMDocument
219
		#實做類別 DOMDocument
220
		$domd = new \DOMDocument();
220
		$domd = new \DOMDocument();
221
		
221
 
222
		#載入html字串	
222
		#載入html字串
223
		$domd->loadHTML($conf["htmlStr"]);	
223
		$domd->loadHTML($conf["htmlStr"]);
224
			
224
 
225
		#正規化html字串
225
		#正規化html字串
226
		$domd->normalizeDocument();	
226
		$domd->normalizeDocument();
227
			
227
 
228
		#取得所有節點的內容
228
		#取得所有節點的內容
229
		$allTags=$domd->getElementsByTagName('*');	
229
		$allTags=$domd->getElementsByTagName('*');
230
			
230
 
231
		#有幾個標籤就執行幾次
231
		#有幾個標籤就執行幾次
232
		foreach($allTags as $node){
232
		foreach($allTags as $node){
233
			
233
 
234
			#紀錄節點的路徑
234
			#紀錄節點的路徑
235
			$nodePath=$node->getNodePath();
235
			$nodePath=$node->getNodePath();
236
			
236
 
237
			#$nodePath內容範例:
237
			#$nodePath內容範例:
238
			/*
238
			/*
239
			/html/head
239
			/html/head
240
			/html/head/meta
240
			/html/head/meta
241
			/html/body
241
			/html/body
242
			/html/body/div[1]
242
			/html/body/div[1]
243
			/html/body/div[2]
243
			/html/body/div[2]
244
			*/
244
			*/
245
			
245
 
246
			#初始化暫存屬性的陣列
246
			#初始化暫存屬性的陣列
247
			$nodeAttr=array();
247
			$nodeAttr=array();
248
			
248
 
249
			#如果有屬性存在
249
			#如果有屬性存在
250
			if($node->hasAttributes()){
250
			if($node->hasAttributes()){
251
				
251
 
252
				#有幾個屬性就執行幾次
252
				#有幾個屬性就執行幾次
253
				foreach ($node->attributes as $attr){
253
				foreach ($node->attributes as $attr){
254
					
254
 
255
					#紀錄屬性與值
255
					#紀錄屬性與值
256
					$nodeAttr[$attr->nodeName]=$attr->nodeValue;		
256
					$nodeAttr[$attr->nodeName]=$attr->nodeValue;
257
						
257
 
258
					}#$foreach end
258
					}#$foreach end
259
						
259
 
260
				}#if end
260
				}#if end
261
			
261
 
262
			#紀錄domd物件
262
			#紀錄domd物件
263
			$result["tree"][$nodePath]["domd"]=$node;
263
			$result["tree"][$nodePath]["domd"]=$node;
264
			
264
 
265
			#紀錄文字內容
265
			#紀錄文字內容
266
			$result["tree"][$nodePath]["textContent"]=$node->textContent;
266
			$result["tree"][$nodePath]["textContent"]=$node->textContent;
267
			
267
 
268
			#紀錄屬性
268
			#紀錄屬性
269
			$result["tree"][$nodePath]["attr"]=$nodeAttr;
269
			$result["tree"][$nodePath]["attr"]=$nodeAttr;
270
			
270
 
271
			}#foreach end
271
			}#foreach end
272
			
272
 
273
		#設置 DOMDocument 物件
273
		#設置 DOMDocument 物件
274
		$result["domd"]=$domd;
274
		$result["domd"]=$domd;
275
								
275
 
276
		#設置執行正常
276
		#設置執行正常
277
		$result["status"]="true";
277
		$result["status"]="true";
278
			
278
 
279
		#回傳抓取到的解果
279
		#回傳抓取到的解果
280
		return $result;
280
		return $result;
281
		
281
 
282
		}#function parseHtmlStr end
282
		}#function parseHtmlStr end
283
 
283
 
284
	}#class html end
284
	}#class html end
285
	
285
 
286
?>
286
?>