[mw_shl_code=php,true]<!DOCTYPE html>! z, m: \% X$ E' R4 L
<html lang="zh-cn">0 u# z3 F+ Z' l' t* ?* i, E; e
<head>
0 f+ u3 z4 `# \! S5 Q/ U<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
0 T# w2 l5 ^' |1 o( D, W<title>測試</title>! ?" C6 L: r4 o" V1 x* n
</head>
+ V! A% T6 }, a: d0 y. d0 y<body>
% U- ~; y n, `# s<?php5 F+ A& v$ p+ q
function get_td_array($table) {+ G* }/ V0 _& i8 Z+ ]! N
//去掉table標籤上的屬性
9 `: j8 s' C7 p* K+ [4 K6 ^ $table = preg_replace("'<table[^>]*?>'si","",$table);
; ]* {3 m8 B- D //去掉TR上的屬性3 h' ]6 t+ O, [$ l
$table = preg_replace("'<tr[^>]*?>'si","",$table);* C! f8 P- f& B3 h
//去掉td上的屬性0 Q) `" }! O1 b. ?) h$ d2 o
$table = preg_replace("'<td[^>]*?>'si","",$table);
; e# G1 |: ?: q //標籤替換
1 _& {, @) Y# [6 Z. ]6 O $table = str_replace("</tr>","{tr}",$table);
8 c/ b v: F1 K6 g $table = str_replace("</td>","{td}",$table);8 K! }" Q; O1 c" ~% `* z
9 w z4 z( \2 d% R0 n( ]" U //去掉 HTML 標記
& `# s1 G' R5 M" ~( j" L $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
) S9 ~9 k/ i* @, z3 J //去掉空白字符
( @* U5 J% Y/ v- x8 b ] K# o1 e. l $table = preg_replace("'([rn])+'","",$table);% i3 r& {, ^0 c ` {5 \, U1 O
$table = preg_replace('/ /',"",$table);8 C" W) y( r5 {7 Z$ [7 {) B
$table = str_replace(" ","",$table);% k8 k4 y# S, o; i. [, [8 Q
$table = str_replace(" ","",$table);
* G, Z' _* X6 ~$ p& t3 s $table = explode('{tr}', $table);
+ X3 D g, p3 Z; l- |1 j7 U: C
/ E% N. y' [. T //這裡打印出來是長度為2的數組。; `% l8 s, n5 W
print_r($table);- e( |! [$ c" b; R( {
//這句代碼有問題,這個會把數組的第二個元素刪掉,所以得不到想要的結果7 y! x9 o% _/ @$ m( t
//array_pop($table);
- ~4 t, y4 Q- o- y( C1 _8 O
! {# u+ X; e4 l' O" N4 K8 w $td_array = array();
4 e; L$ y8 i. L# |" l8 G* E( A2 Q ?7 h
6 n8 x# c3 H4 B" U3 P ^ foreach ($table as $key=>$tr) {. x. g1 F7 d0 B5 M p. d0 W
$td = explode('{td}', $tr);& o! D$ ^% l# o, S7 B4 G
array_pop($td);
. G3 c# ]9 n5 } $td_array[] = $td;
, `; f$ Y0 m7 ^ }
9 E: m# q6 d0 Q& z$ w( v return $td_array;" t7 ~* { U$ ?
}
; P- }/ Q7 r6 L2 V" [//下載HTML網頁# {$ K8 z% s" i7 B5 X% s+ a
$s=file_get_contents('http://chengji.lideping.com:7000/exam/query/query.jsp?logname=20133011235');
" {3 _) |" A9 `' k& o9 D" D7 r$s=mb_convert_encoding("$s", "UTF-8", "GBK"); //編碼轉換
4 Q) M" K. w0 W; g" v3 X//獲取最後一個table內容7 u: X( ]( Q2 P' Y. h/ `
$s=substr($s,strrpos($s,'<table'));7 Q9 m. q( ?( t& h8 C+ y# f
$s=substr($s,0,strpos($s,'</table>')+8); V6 T. e3 L+ M- k% R8 a3 w% A
//刪除超鏈接
+ q3 P% o+ B E8 c$ F3 u$s=preg_replace('|<a href.*?</a>|','',$s);
1 f. d# I" A; P& b+ d- \' d$s=preg_replace('|<td align=\'center\'></td>|','',$s);//刪除多餘空格4 R, Z% M, x! X# g4 S6 e/ B
7 O. I8 Q- U9 `9 }* Y$ \2 E
echo $s;- [+ O: M t8 P% ?8 d' g1 @
preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出
* l4 A$ p% d! _/ ^! T) e; r( [; h% V4 E% l3 c$ t
$arr = get_td_array($table[0][0]);//執行函數3 d5 l/ S$ f2 W0 v
print_r($arr);6 k: U0 @1 h5 \
?>$ C T+ r) }" O7 q; K# Q; |
</body>
/ D+ d( s4 L" j) [$ A% t) \8 C# u8 V</html>
/ l# g) {6 |: q* w[/mw_shl_code]( E# l: {0 X5 [' l3 p' W5 w2 ]4 r
! a5 i# f5 t' B已調整完全正確輸出,給樓主你註釋了哪句代碼的問題
( \4 R: s+ x' h) e$ M4 z7 a- X |