[mw_shl_code=php,true]<!DOCTYPE html> C# @8 d6 w1 Z+ W# W
<html lang="zh-cn">* O% D1 V$ Q \/ U. _
<head>0 p& M* V, _) k5 _1 N
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
; c3 h+ J! N7 ~/ x5 l<title>測試</title>
1 R p! b/ v ~' s5 m</head>0 ~+ E# X9 p6 z! B, {3 P6 `% [
<body>7 q7 n7 c4 E% v' m* \: }* ]3 m3 W
<?php
?* W) r* v2 _3 Y5 n. u' ~7 ufunction get_td_array($table) {1 h l! X( D+ N$ U
//去掉table標籤上的屬性' x: t3 R* J7 [
$table = preg_replace("'<table[^>]*?>'si","",$table);" _* B! c# Z& ~4 G
//去掉TR上的屬性; f6 ~+ }9 J6 k9 G: K. Z5 |( |
$table = preg_replace("'<tr[^>]*?>'si","",$table);& Y% { C: Z: j% {( T5 F& |
//去掉td上的屬性! i: A6 d9 R+ Z' [- F3 X3 e' N- T" m
$table = preg_replace("'<td[^>]*?>'si","",$table);
6 B! l; ~! |7 r //標籤替換
# t2 V m" @% v `' x* ] $table = str_replace("</tr>","{tr}",$table);
: Q( i5 p j% l6 O$ E& W, I $table = str_replace("</td>","{td}",$table);
! X+ H0 E, j9 L- h8 _+ f! W+ f, r/ o2 F, Y: T4 a0 i
//去掉 HTML 標記
/ ]( s' _; j6 ~- ^! o* @2 q $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
& ?# L3 g0 Z, ^4 |9 n //去掉空白字符 J7 s; V: e9 o* k- Y+ ^
$table = preg_replace("'([rn])+'","",$table);
, n# R7 g R) r5 v: O1 X $table = preg_replace('/ /',"",$table);9 \% ?' c" q# F! C8 x1 {" o
$table = str_replace(" ","",$table);
, Z9 A4 L4 ]( R* v+ S/ y $table = str_replace(" ","",$table);
2 h; m w$ f% m4 s $table = explode('{tr}', $table);
; Z0 n, B2 h/ ?
; ?( Y8 K# i' ]5 l, K" J/ f //這裡打印出來是長度為2的數組。3 O* c- ], F, R$ n0 [
print_r($table);
3 D5 q3 B* }1 z( d) g, x //這句代碼有問題,這個會把數組的第二個元素刪掉,所以得不到想要的結果
5 ?+ `7 l' T0 w3 l$ x. M //array_pop($table);# i9 }) Z# ]. g* \6 R3 ]: G
0 G6 e$ C4 ?5 ]6 g $td_array = array();
6 @; l- z& n9 ? C
# {: ^% S, T7 Z; M6 N foreach ($table as $key=>$tr) {
" v. r& h \" V- k6 @( e0 N, p $td = explode('{td}', $tr);
6 _3 B! W6 R' Y array_pop($td);1 V' h4 Y" o9 C& J2 c6 G
$td_array[] = $td;) Z/ j9 [: |+ H J, {
}
1 f3 G: { Z* t1 S return $td_array;
6 ^1 p" V& r5 g9 Z" U! f}7 y0 X: S1 ^8 E J; u
//下載HTML網頁. M3 T. n4 S: n2 m. i d" w
$s=file_get_contents('http://chengji.lideping.com:7000/exam/query/query.jsp?logname=20133011235');2 x1 {' o6 N3 W' o \4 _% [* m
$s=mb_convert_encoding("$s", "UTF-8", "GBK"); //編碼轉換4 q1 P1 _# p, ^( {7 Q
//獲取最後一個table內容
5 _" u' k7 X; r' ~3 [$s=substr($s,strrpos($s,'<table'));
. ]2 [% T5 q* z6 ]. o0 ~6 _; o$s=substr($s,0,strpos($s,'</table>')+8);
& K1 c8 Y2 P i; s//刪除超鏈接
" q* D( P# z4 V, m! O' q+ a) Y8 `; z$s=preg_replace('|<a href.*?</a>|','',$s);
/ U- S, l' [" s/ H2 _0 j* T/ X$s=preg_replace('|<td align=\'center\'></td>|','',$s);//刪除多餘空格
+ C: l4 \# b5 H/ B: d8 V$ S) c& @: R" w- Q$ ]! ]
echo $s;
' n& }5 N9 b* n# Q- r7 Ypreg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出1 A$ Q1 \0 n& e+ O$ h' }
! ^/ Y+ n: w- Z t5 D, \( V0 T
$arr = get_td_array($table[0][0]);//執行函數' |6 W- Q* e: Y- P
print_r($arr);
; Q- @* z1 N' ~' Q- o- g" l& c8 q?>) N$ e- J$ Z4 H8 n
</body>( `. y; r& [$ {5 r/ v% v
</html>* G% |$ U' Q1 ^ W# o, y# B% B
[/mw_shl_code]
) m3 x' C0 n" W& J! K; N1 M
$ ]" z. v0 U% }1 M3 }5 R0 n& I已調整完全正確輸出,給樓主你註釋了哪句代碼的問題
+ E; ?; W& T) D |