[mw_shl_code=php,true]<!DOCTYPE html>
c8 g4 p8 s5 z* j2 _! A( J<html lang="zh-cn"># U, |) e6 ^ H3 B
<head>7 f" _; A: i" }9 z7 `4 M
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />5 R( j8 v3 g. `3 b- H
<title>測試</title>0 l: X/ m R0 N8 b& l
</head>- ^1 u9 Q4 P% |6 p" N7 t
<body>
- O& [4 n6 D" y<?php4 U. \4 T- D9 I) l4 p+ W- t4 h
function get_td_array($table) {, ~. f, z7 D8 {$ R0 }, M- x
//去掉table標籤上的屬性
/ v( ]5 v+ L' l. i $table = preg_replace("'<table[^>]*?>'si","",$table);
: q6 F/ x6 a8 w6 D6 i8 E //去掉TR上的屬性3 R+ V/ c$ } `2 l
$table = preg_replace("'<tr[^>]*?>'si","",$table);
0 l; X; h: n/ n //去掉td上的屬性: _5 K5 e& g7 M
$table = preg_replace("'<td[^>]*?>'si","",$table);
4 a- y: m9 {% n$ q //標籤替換( i2 {- u" N* T3 I
$table = str_replace("</tr>","{tr}",$table);0 B+ `1 |# b5 J, b# r+ e
$table = str_replace("</td>","{td}",$table);, @: ~8 b) G+ q
: _0 n* U' B5 W2 E2 x //去掉 HTML 標記7 n+ v* v; t h+ ~' f; k7 ]) p
$table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
( L2 B3 G8 v. K X% L //去掉空白字符9 J& d0 B N; Q1 W( X
$table = preg_replace("'([rn])+'","",$table);. C9 _) i1 W, f* y
$table = preg_replace('/ /',"",$table);
3 n( Y9 \5 Q) @% C9 [7 S $table = str_replace(" ","",$table);
5 }0 l' R8 } I9 M! q( X $table = str_replace(" ","",$table);+ v3 F) g, ^" E% T! p1 L; H& j7 I
$table = explode('{tr}', $table);
, M3 j _$ t' G1 U- J% |) ?" U8 n, A8 J: D- k
//這裡打印出來是長度為2的數組。( ^! a, `% [6 D7 W. s! o
print_r($table);
3 |& L3 d! K: c8 V5 e //這句代碼有問題,這個會把數組的第二個元素刪掉,所以得不到想要的結果
" F2 `$ Y7 ^ D+ o' L9 g //array_pop($table);
3 L' ^" G4 b. ?% L
/ D" m- H: n: m7 s5 o. C $td_array = array();
0 u! E& r- Z4 m8 K6 |; \
- W' [4 H, @+ b. u7 k; v3 R$ e W, M foreach ($table as $key=>$tr) {
$ `$ u5 T! K& a4 _% S1 K1 ?& ~! w $td = explode('{td}', $tr);
5 e) y y7 B0 l array_pop($td);* F2 v9 k& ]9 k8 x* ~
$td_array[] = $td;
7 P: g' P4 ?6 \5 d1 ^" x }; |9 c8 X; f4 F* d! B! F: A; j
return $td_array;" K4 k3 O" M C. R- h. z
}
7 V+ M$ c2 ~0 e7 d" ~0 @//下載HTML網頁8 e* X6 x/ ?% b: s0 F; c
$s=file_get_contents('http://chengji.lideping.com:7000/exam/query/query.jsp?logname=20133011235');
" \: l% x; I- K! O$ W2 N$s=mb_convert_encoding("$s", "UTF-8", "GBK"); //編碼轉換( w9 n5 T6 Q8 C$ F
//獲取最後一個table內容9 \! Z7 E) {' L' m
$s=substr($s,strrpos($s,'<table'));
2 i2 F- p5 h3 V* ~$s=substr($s,0,strpos($s,'</table>')+8);
; @7 G. `; q2 a; R3 B//刪除超鏈接0 j7 C% V- u: r3 ^
$s=preg_replace('|<a href.*?</a>|','',$s);: r7 n7 S+ K, `0 Y: l7 J
$s=preg_replace('|<td align=\'center\'></td>|','',$s);//刪除多餘空格
- ]0 Z7 R+ U8 c( _6 p! [- H p$ p5 Z: _( }) q( `
echo $s;! n M' ?3 b8 ?( U- e
preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出
$ f/ g$ d' O# s v* {3 X9 L8 |: y. P7 L& T. C" H2 P
$arr = get_td_array($table[0][0]);//執行函數
5 C Y8 _4 p0 Q( N c) K. Hprint_r($arr);
: X; e* l8 t% j& w" S?>
) w8 `, G0 m6 W% |</body>* ^, W5 X3 A( v$ ^- j
</html>
0 X+ e( [' c O/ I* j& |$ t z# p[/mw_shl_code]5 s% z$ ?& k! b; u
7 i7 G# @! c9 e! T0 y3 o+ u已調整完全正確輸出,給樓主你註釋了哪句代碼的問題( }( {' \0 o, x. q0 g
|