[mw_shl_code=php,true]<?php
& W7 e9 L9 h8 b" ?& M/ m. X* ?+ uheader('Content-Type:text/html;charset:Utf-8');
8 F' q! X$ L3 ?# _" x3 z1 zfunction get_td_array($table) {
* j& ]* I- Z1 T" u. K2 o $table = preg_replace("'<table[^>]*?>'si","",$table);
; K- x- E2 I3 }* z% ~$ T/ k2 r $table = preg_replace("'<tr[^>]*?>'si","",$table);1 y$ t. B( Y, k2 W* m
$table = preg_replace("'<td[^>]*?>'si","",$table);5 e3 d/ {# \& ]* |6 j3 A+ Q) S% f$ L
$table = str_replace("</tr>","{tr}",$table);. M8 e* N6 Y$ k
$table = str_replace("</td>","{td}",$table);
* F- c% W& x+ r# F //去掉 HTML 標記! T S) F5 _3 ]' q' o) v, \1 o
$table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);3 x6 E/ u: B5 _9 P2 \/ b _
//去掉空白字符; A; P9 t7 P- e$ ~0 g7 O
$table = preg_replace("'([rn])+'","",$table);
4 r9 g6 T, M; {; [, W $table = preg_replace('/ /',"",$table);
" ?' f2 o) i6 h6 I: w3 v) A. f $table = str_replace(" ","",$table);
6 r& b7 U+ w0 a# R% X) q9 f- E8 J $table = str_replace(" ","",$table);
& S, U7 O% V1 l. D6 i& y $table = explode('{tr}', $table);7 V- g( l0 ^3 [( Q, @: f
array_pop($table);8 H- d' |6 e' S/ b- ^1 A% Y8 Q8 I
foreach ($table as $key=>$tr) {' t t! N6 e7 p8 }
$td = explode('{td}', $tr);4 P7 R' l5 X: k6 C# u
array_pop($td);
C9 o; H* T, `; Z/ w; n) C $td_array[] = $td;
2 Q ?( k, S; d& B( j+ f. [0 Z" [ }* c5 u. y, S8 O
return $td_array;4 L: T0 P& X6 c& f
}
T2 ^/ n- d8 d' } //下載HTML網頁( s# Y8 B1 ]7 j) {2 [
$s=file_get_contents('http://222.88.107.92/exam/query/query_detail.jsp?examid=2014102014&paperid=100094');# f* A! V) U+ {, d' {- x \
$s=mb_convert_encoding("$s", "UTF-8", "GBK"); //編碼轉換
% y) {5 t. v( X //獲取最後一個table內容
* T+ F7 E. ~$ q. W! p $s=substr($s,strrpos($s,'<table'));; ?- v* Q7 c& @ d
$s=substr($s,0,strpos($s,'</table>')+8);$ r& I5 h* s7 h, q* R# I- Y/ I
//刪除超鏈接
; A6 T. v) ? d N4 ~ $s=preg_replace('|<a href.*?</a>|','',$s);
" t+ n5 ~) ^% B $s=preg_replace('|<td align="center" width="120">圖片</td>|','',$s);
' }* P+ @* l! ~: W9 { $s=preg_replace('|<td align=\'center\'></td>|','',$s);" @8 g. N4 P V8 V' O% K1 I6 }
$s=preg_replace('|<td align="center" width="120">評閱老師</td>|','',$s);
1 o$ H. a5 M1 i+ O" X; T# x. F* [7 G$ } $s=preg_replace('|<td align=\'center\'>110(.*?)<br></td>|','',$s);
/ |* m+ [% A% C echo $s;0 Q; G. A% J9 n* `1 | x
preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出( r) y! D) ?8 n- h
$arr = get_td_array($table[0][0]);//執行函數/ |; |, ]+ X: p: f" {3 U6 N( `
print_r($arr);$ @9 r2 p4 |0 M! |% \
?>% U0 B" A4 A! ]- q! F
[/mw_shl_code]
2 ]; m' q! o6 T* B+ O- A9 g+ u |