[mw_shl_code=php,true]<!DOCTYPE html>) b- h8 a, p2 G8 v" X
<html lang="zh-cn">
) }' R7 H( a. O" t9 {; u; U<head>; C' \1 Z, I0 |; a' h, j
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
' f( Y1 h, t: |! Z) a8 F& o+ m<title>測試</title> B+ Z( t3 Z) h6 L* ]9 F, i8 b
</head>1 r2 ?: w/ p$ G4 v1 }9 G3 a
<body>
# a/ \( K6 j3 ?3 L O/ b. y3 G<?php" l- a& g. v Y4 ~8 J% @
function get_td_array($table) {0 L& u3 O* k# k% H j
//去掉table標籤上的屬性( |+ n3 e. I. O ^) e: U
$table = preg_replace("'<table[^>]*?>'si","",$table);6 a8 c' t4 Q- o
//去掉TR上的屬性2 G! T' R% F) S$ U- ?# m, G
$table = preg_replace("'<tr[^>]*?>'si","",$table);: L; a% f$ u( g$ L0 a3 A
//去掉td上的屬性
8 w3 S& V {) _ $table = preg_replace("'<td[^>]*?>'si","",$table);
* o* U; i" D: ?9 s //標籤替換" X' A6 ]5 G6 Z/ L; z8 k/ I( }; f% n
$table = str_replace("</tr>","{tr}",$table);
( J/ C; \+ N n $table = str_replace("</td>","{td}",$table);: R. D% E0 @# { _
! A' X: K$ Y9 n! W! S3 n. B5 h* v$ w1 @& m //去掉 HTML 標記! x# f% q% G/ Y% j. o1 ?- P s
$table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
) ]7 {+ l0 `* J# G$ D //去掉空白字符
& |& A0 y( U, a ^6 y" f: d5 E$ S( q $table = preg_replace("'([rn])+'","",$table);
' L- b! l3 H' F# E: e $table = preg_replace('/ /',"",$table);
7 m/ s2 Z; B! _* ]5 ?0 s; Z5 q. K- ? $table = str_replace(" ","",$table);
- h% L, ^' z* K4 a3 @( D$ `6 l# U q $table = str_replace(" ","",$table);6 g0 q- c0 t. Y
$table = explode('{tr}', $table);
; U- B3 ^' C8 i3 s/ s) X0 O/ ?/ Z/ ?, r
//這裡打印出來是長度為2的數組。
1 @" q( z. I R print_r($table);
, ?' u' ^! S# {8 z1 _ //這句代碼有問題,這個會把數組的第二個元素刪掉,所以得不到想要的結果4 {$ y+ v( R7 R7 O0 I( ^- [
//array_pop($table);' n6 D5 i, u& L( P$ `
( ]1 _/ ^4 L4 i5 K& C- F$ x. m# e
$td_array = array();" k$ T, F) f0 c2 r7 }
$ M( R2 X w K+ ^2 S, Y
foreach ($table as $key=>$tr) {- o4 z, E! W7 G3 l) G
$td = explode('{td}', $tr);* x3 c. D) X- f* k( f6 X
array_pop($td);# v+ j# R4 ~5 O; n
$td_array[] = $td;
9 L- r5 q0 ^& h$ f2 } }; s& K8 J( b) w9 g: T1 z8 P. _
return $td_array;+ `) L8 s/ w& e8 Y" |* N S, Q
}& S; e% e/ h# W5 ]; f
//下載HTML網頁
$ n, y) x% N# I1 z) A& X, P( Y$s=file_get_contents('http://chengji.lideping.com:7000/exam/query/query.jsp?logname=20133011235');: t7 @- y3 a C, ~
$s=mb_convert_encoding("$s", "UTF-8", "GBK"); //編碼轉換
+ k3 F. b. s3 b3 j0 S, f: Y3 Y5 b//獲取最後一個table內容
l t; t. k" L+ W" D1 B2 R$s=substr($s,strrpos($s,'<table'));& r+ a1 s b3 W. T
$s=substr($s,0,strpos($s,'</table>')+8);( ^/ k8 r( y* {( Y, u3 T7 u
//刪除超鏈接
t8 t! e" c' u$s=preg_replace('|<a href.*?</a>|','',$s);8 P: G9 V' \1 E; Q
$s=preg_replace('|<td align=\'center\'></td>|','',$s);//刪除多餘空格0 j! \" X" a) M! y# y ]: ^+ Z8 d
3 A4 I8 f1 ?& y7 Xecho $s;/ S1 e, ^; M) O6 M. j2 N' C
preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出
& h1 i# [6 D$ T
/ h# a$ H1 [6 n2 K) @$arr = get_td_array($table[0][0]);//執行函數
& p6 t2 K- e1 m: V' v+ kprint_r($arr);( n/ Q' Z4 o9 J: K
?>2 ? ?) t3 w7 V6 @! O
</body>4 m; U9 |- n4 \4 T7 H
</html>: F6 u3 @7 Q1 p9 }9 o+ P6 u
[/mw_shl_code], r$ ~$ M+ B8 {7 M8 @
1 T- _( V: g" P2 r( E( z
已調整完全正確輸出,給樓主你註釋了哪句代碼的問題! t) i8 {: B9 A* c, ]3 W0 }
|