[mw_shl_code=php,true]<!DOCTYPE html>
+ z: `( h: ?; y% F& D<html lang="zh-cn">9 @* D- g4 y+ s7 G
<head>
# {6 V# ~1 r: J @' |+ k<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />8 Z& k' g4 l% ~# K& l, k
<title>測試</title>2 Y9 m a/ R+ C# X3 P6 Y! d1 _
</head>' [( `; d# [+ y+ L# H
<body>' x# X W3 O# Q3 F! ~6 {
<?php' S; e8 k7 n% D! L
function get_td_array($table) {0 U5 L5 a7 i8 c" I( E% ]: C! C
//去掉table標籤上的屬性% B. w! p6 Q" N' @6 @7 t/ `% @4 w
$table = preg_replace("'<table[^>]*?>'si","",$table);$ A" n* F$ k4 y* }9 Y
//去掉TR上的屬性' g' e/ k- p% o. T9 n1 D
$table = preg_replace("'<tr[^>]*?>'si","",$table);( J f( p; F9 _# V5 ?
//去掉td上的屬性8 a2 R/ `# P/ D7 z$ {$ l$ T
$table = preg_replace("'<td[^>]*?>'si","",$table);: _5 S0 T o1 h
//標籤替換
s* s* I; N/ k) [& d6 R% H, l. G $table = str_replace("</tr>","{tr}",$table);
4 u: n& d( q O% K; `! S( P6 Y $table = str_replace("</td>","{td}",$table);
* T7 ^% h, B+ s3 E# _7 H" k& {9 n8 v: r S# K' H% I8 g, H' K
//去掉 HTML 標記
* w1 X; V" ?& ~5 _# x( z $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);0 U4 W4 O i) b) Q5 j. h! m% s
//去掉空白字符
j% U, G/ d- s) v, t2 Y, [ $table = preg_replace("'([rn])+'","",$table);
, l& L- G* d$ w. n3 n% C7 }/ `) k $table = preg_replace('/ /',"",$table);
G, F2 S" x* q) s- J $table = str_replace(" ","",$table);6 m, o: D( g7 s& N; f
$table = str_replace(" ","",$table);
/ s% y8 J4 }6 {* a: N7 J" {1 R $table = explode('{tr}', $table);
/ k0 L: m1 O# Y2 L8 [- f1 s% A" `- x3 }1 d
//這裡打印出來是長度為2的數組。
! ?8 `" ]4 L9 Y3 M print_r($table);
$ a% s# D8 ^4 q: l {8 a //這句代碼有問題,這個會把數組的第二個元素刪掉,所以得不到想要的結果
. y8 ` W8 D" W0 ^1 u1 `5 e( v //array_pop($table);4 ~+ ^6 O" N% [, K
1 C, t6 m6 f. e! v; e
$td_array = array();8 B9 p0 ]3 S( B% m1 l" X
g# x% t! Y+ v1 T5 J; T foreach ($table as $key=>$tr) {
3 U3 s+ f7 A! E, G) d $td = explode('{td}', $tr);4 s( \6 b, }6 u
array_pop($td);
; X u c7 T& d0 F/ z7 u: Z2 [$ H6 Q( n $td_array[] = $td;
7 i5 J) I1 b5 n7 l }
9 _; g# u$ a6 H- y' x9 C return $td_array;: C2 f% q- F: z/ y+ b& ?
}
( z+ W7 }' n0 h2 t) I2 I) F5 s//下載HTML網頁
# o+ k! g5 T+ X, {3 ~2 M% h$s=file_get_contents('http://chengji.lideping.com:7000/exam/query/query.jsp?logname=20133011235');
* `' X9 c/ {# @$s=mb_convert_encoding("$s", "UTF-8", "GBK"); //編碼轉換5 x$ ], G5 C: B* R+ U- X2 V+ K
//獲取最後一個table內容7 U9 f! T% j6 T
$s=substr($s,strrpos($s,'<table'));1 h" R) ^2 \, H; A
$s=substr($s,0,strpos($s,'</table>')+8);& H; I9 |5 t/ K. k
//刪除超鏈接
( d( V; b5 u2 C: W/ B9 T$s=preg_replace('|<a href.*?</a>|','',$s);
& \0 o$ @: h# h2 _$s=preg_replace('|<td align=\'center\'></td>|','',$s);//刪除多餘空格$ `3 w* l7 U: w& m5 J8 Y* a
2 q! B- e0 h2 V5 K1 n5 Aecho $s;
w3 n, S% y+ ]+ d7 C4 kpreg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出# n6 W' x) m$ B! W/ L
. W& H! z4 b5 q$ W0 r$arr = get_td_array($table[0][0]);//執行函數
9 Z5 K% L4 b! A9 |( A" [( @print_r($arr);4 E+ M0 A7 A Q# G# ^
?>8 o# Z, n1 x! \3 F% J' T1 Q; J
</body>9 h* o7 ~8 L' ]# k0 c
</html>
, R- C ~6 B- q4 \0 t, Q) [( [[/mw_shl_code]+ h( _! A% p& c
. L% s( R- v- c1 j6 n% a已調整完全正確輸出,給樓主你註釋了哪句代碼的問題. {: Y; u9 y9 {$ R3 q$ J
|