[mw_shl_code=php,true]<!DOCTYPE html>8 ^8 A$ T7 F* I
<html lang="zh-cn">
4 y. D- h# E( @5 M! R# H<head>
: a3 N$ j4 B; i9 F9 O<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /># j, c' [- C/ P9 {$ l3 \
<title>測試</title>3 l3 f1 F2 h. w2 H
</head>
. ^( R4 s# h: W, M& C<body>
+ m+ I' v+ E- @$ F1 Q. T<?php
* X, {$ y' X0 i) q% h' E8 Tfunction get_td_array($table) {
# P( ~8 U# B7 N7 H' \# z F //去掉table標籤上的屬性
6 ^8 K- c1 J% [$ n" V8 r $table = preg_replace("'<table[^>]*?>'si","",$table);. [, D8 {) q: ^+ K/ a
//去掉TR上的屬性
' U: I X2 A! h- R# v $table = preg_replace("'<tr[^>]*?>'si","",$table);5 O& W& w0 \0 i" ^+ t% H3 c
//去掉td上的屬性; d4 f/ @$ c( |& Z7 n
$table = preg_replace("'<td[^>]*?>'si","",$table);
( c, ~0 h' Z5 D //標籤替換! _4 T! R! r1 {) `, a. y) c3 ~' k7 a
$table = str_replace("</tr>","{tr}",$table);
2 B t* W, Q; O. H3 Y& r7 C $table = str_replace("</td>","{td}",$table);( e3 ~+ u: p& @3 M* g
+ u6 I/ S# c% {& k' P
//去掉 HTML 標記: @0 J4 p# k! p0 r
$table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);3 e% m3 F9 I8 d# ?- _9 y
//去掉空白字符
4 C- l; R4 o2 I+ P( E7 J9 E2 Z $table = preg_replace("'([rn])+'","",$table);
( L% K& {6 N* E0 h4 Z $table = preg_replace('/ /',"",$table);
* s, N% F ]& s2 B( l" b- P $table = str_replace(" ","",$table);
7 o7 t J6 J9 }6 G $table = str_replace(" ","",$table);" @6 L( ` F! V
$table = explode('{tr}', $table);/ L d: T; c: O" V8 T; w1 [
" g( P! D" T" h- k1 D //這裡打印出來是長度為2的數組。
' U0 F+ {/ M6 X9 h, O0 p! A' w print_r($table);
/ n4 F, K8 {4 b3 f" Y //這句代碼有問題,這個會把數組的第二個元素刪掉,所以得不到想要的結果( {1 l' g/ u- b9 E' | Q
//array_pop($table);" g) r5 _: m5 c4 E# r( U6 H5 |
7 c# n. ^: N1 E. }: W9 v $td_array = array();, T. N x5 l6 c% u
& c/ f+ L6 y, N- G foreach ($table as $key=>$tr) {0 v" i8 V: h, ?9 [3 q& ~9 o+ D
$td = explode('{td}', $tr);
% W% r) U! g( Z array_pop($td);
5 h1 o0 @8 t' k$ V2 z# g $td_array[] = $td;8 D9 a9 R2 p5 l; f% \/ k: v8 ]
}( t/ W9 v% ]* ^# A
return $td_array;
c# I8 K& ^& G2 u v}
2 U5 G. `: c( \//下載HTML網頁3 Z% @2 c$ K# B4 l* Z6 Z
$s=file_get_contents('http://chengji.lideping.com:7000/exam/query/query.jsp?logname=20133011235');( M6 X5 e9 e4 q. B
$s=mb_convert_encoding("$s", "UTF-8", "GBK"); //編碼轉換& D6 B( G: }: a0 ^
//獲取最後一個table內容
8 A% K! e/ l+ z& ]) r$s=substr($s,strrpos($s,'<table'));
3 j1 N$ k, p( I6 E5 V: K$s=substr($s,0,strpos($s,'</table>')+8);8 A8 t$ _* z0 P! d% l
//刪除超鏈接
+ ^8 d: z; c, ~% f7 b$s=preg_replace('|<a href.*?</a>|','',$s);
2 G6 _- C2 c: z6 @7 g2 W" i$s=preg_replace('|<td align=\'center\'></td>|','',$s);//刪除多餘空格
) [2 _& X- d$ }, J' O6 c5 e+ b$ h9 U& o" z
echo $s;7 f" x4 V* u& P. }8 u/ {; Q
preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出
5 o/ B. ^( O* L8 B# ~; A* W7 \6 I. i9 j1 W7 h
$arr = get_td_array($table[0][0]);//執行函數
( Y$ g; s- t, _3 @' E; T2 xprint_r($arr);
}0 r. v! R8 }3 |?>
4 S1 U/ `0 w7 ~; T% ]</body>% {1 y- a. z* r! M0 z7 i1 A6 [
</html>+ J3 J. {/ Y7 \" n. R, O
[/mw_shl_code]: r& p5 S. j, j9 I! g6 y
U1 I" i" ]# Z# k已調整完全正確輸出,給樓主你註釋了哪句代碼的問題
+ B% M+ t! m$ L |