[mw_shl_code=php,true]<?php( ?' } ]( D3 f5 y( K: {
header('Content-Type:text/html;charset:Utf-8');4 S7 l7 m& f" R! w
function get_td_array($table) {, N2 E, ?+ O* s
$table = preg_replace("'<table[^>]*?>'si","",$table);
, s+ U; t& L9 K* w& v $table = preg_replace("'<tr[^>]*?>'si","",$table);
3 h! O* E% X# ~; U" y4 d N4 A $table = preg_replace("'<td[^>]*?>'si","",$table);3 P' r5 @, S/ v; P. E$ T
$table = str_replace("</tr>","{tr}",$table);
n4 ?: d" a# y1 o5 [ $table = str_replace("</td>","{td}",$table);" M. h, }5 R3 G2 l. }
//去掉 HTML 標記8 j9 z2 D3 }+ Z4 V( X
$table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
6 J8 r; t9 a: r0 p4 I //去掉空白字符- I- s$ |0 d6 G+ O6 x* `- ~7 Z
$table = preg_replace("'([rn])+'","",$table);
$ ^, _$ _1 w$ |* T" v, o$ e/ ~+ U/ w $table = preg_replace('/ /',"",$table);9 [6 H& `/ y" F$ S0 g
$table = str_replace(" ","",$table);: I/ x, `+ F1 w, T( C* X+ g
$table = str_replace(" ","",$table);/ n5 L% l4 R6 a' B
$table = explode('{tr}', $table);
8 u! i' y! K: F6 ~" M1 ^; [ array_pop($table);7 n- p! Q7 {1 ~0 d
foreach ($table as $key=>$tr) {4 N0 H7 L6 Z* `. Q( W
$td = explode('{td}', $tr);
! s8 ?1 e0 r5 M5 y+ Z array_pop($td);
4 C$ ]4 w$ |6 H2 z! ]* c $td_array[] = $td;
0 ]% f! S! j3 n) ~ }+ D( v; G+ F9 r4 n. q
return $td_array;8 d* o# E! z) K- y- c6 s8 P; h- c4 m
}
, B n5 |# ]2 P) F* E //下載HTML網頁
" L4 u1 B4 I, z $s=file_get_contents('http://222.88.107.92/exam/query/query_detail.jsp?examid=2014102014&paperid=100094');) |- M$ m! k# I
$s=mb_convert_encoding("$s", "UTF-8", "GBK"); //編碼轉換
c7 o6 T% I8 |% k2 B: n" a //獲取最後一個table內容
; j* G7 M! C' ?( i" p/ `* G6 y $s=substr($s,strrpos($s,'<table'));
6 O- @9 R$ j4 o& E/ u8 E $s=substr($s,0,strpos($s,'</table>')+8);% S9 L* F: c, K1 @
//刪除超鏈接
- L( l8 }. V" N5 a) h" s $s=preg_replace('|<a href.*?</a>|','',$s);0 D% p2 [1 l# |8 P
$s=preg_replace('|<td align="center" width="120">圖片</td>|','',$s);7 D. q* h5 o" M. Y
$s=preg_replace('|<td align=\'center\'></td>|','',$s);
3 M6 p5 | O& d7 F% D4 B( r $s=preg_replace('|<td align="center" width="120">評閱老師</td>|','',$s);1 G! i$ ?6 s1 @4 H! a7 c) ^* _
$s=preg_replace('|<td align=\'center\'>110(.*?)<br></td>|','',$s);" U* P- n3 \, g* Y6 ?* Q
echo $s;9 ^& n, r8 Y3 w7 c
preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出 g" t: p. S( h# V
$arr = get_td_array($table[0][0]);//執行函數
* Q& Q: X0 m2 X; p print_r($arr);: H9 x. T0 V! T; g
?>
" H8 ~* e7 G6 Y[/mw_shl_code]
6 T% d1 |- M% F1 i4 ?
|