过期域名预定抢注

 找回密碼
 免费注册

求大神。。。

[複製鏈接]
發表於 2014-10-21 21:56:41 | 顯示全部樓層 |閱讀模式
懸賞20點點已解決
本帖最後由 xx19941215 於 2014-10-21 22:02 編輯   Q7 I( p' }$ x3 P- P4 s

+ @( {7 ?: Y* P6 x! l代碼[mw_shl_code=php,true]<?php
: T" u5 v# d5 V9 s* G, P/ K. O$ p1 Bfunction get_td_array($table) {
4 y% W0 [& h6 z1 ]4 u. ]        $table = preg_replace("'<table[^>]*?>'si","",$table);% ^6 Q% \/ V. ~$ Z' J% g
        $table = preg_replace("'<tr[^>]*?>'si","",$table);
( y9 q4 T& U8 Y0 p$ `        $table = preg_replace("'<td[^>]*?>'si","",$table);+ k$ a% r" }, U7 R" v( y
        $table = str_replace("</tr>","{tr}",$table);
! b. q# U9 \, {6 b        $table = str_replace("</td>","{td}",$table);
0 D: \& q  C+ ?" ?        //去掉 HTML 標記
, m' [# X. Y: ?+ D  S% `        $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);4 t- p5 h% W9 \  K- s% n
        //去掉空白字符
8 Y5 H( I' [! r) I        $table = preg_replace("'([rn])+'","",$table);" n. S; ?' a# d. C) T1 J* B5 z" h
        $table = preg_replace('/ /',"",$table);
1 H! k" z  h# X$ L+ K1 l6 O' ?/ m        $table = str_replace(" ","",$table);1 W$ k3 r3 [5 |$ C& t+ o. t
        $table = str_replace(" ","",$table);' `8 [7 D+ R4 N- b  P8 G4 G8 h! i
        $table = explode('{tr}', $table);
$ _1 O& b2 y$ x& V+ e( T( P        array_pop($table);
6 t( O! m/ ]$ ]+ D6 d- a        foreach ($table as $key=>$tr) {
5 v- W2 [# Z7 M                $td = explode('{td}', $tr);
! T0 B1 O+ u0 U, T* G$ u/ M4 `6 L                array_pop($td);! f3 r- X8 `1 @- ^& U% n' O
                $td_array[] = $td;8 f& l  L# P" Q; F& G  v
        }5 _" `& S- E1 ?' {/ s+ G0 k' t) b
        return $td_array;; G) u+ t0 l" U+ g1 f4 j$ W
}
+ R* }+ V  `( d/ z/ O6 G  //下載HTML網頁
; B' X; R4 w. ]: M7 T- v0 ^: @  $s=file_get_contents('http://chengji.lideping.com:7000/exam/query/query.jsp?logname=20133011235');
7 R! Q+ }: k- W4 q$ \) z  $s=mb_convert_encoding("$s", "UTF-8", "GBK");  //編碼轉換
* e- R4 m5 {9 z4 {: ^2 {6 `7 W  //獲取最後一個table內容
. n7 G1 f; G6 T: S; p% ^  $s=substr($s,strrpos($s,'<table'));
$ w$ B' G/ H% ?  $s=substr($s,0,strpos($s,'</table>')+8);9 `" t1 l  n6 x5 ?
  //刪除超鏈接
/ ^1 l2 R  }/ B: r2 l  $s=preg_replace('|<a href.*?</a>|','',$s);7 Y. |5 y0 i7 \' s( U+ G) s
$s=preg_replace('|<td align=\'center\'></td>|','',$s);//刪除多餘空格% d. U5 ?  U2 \" u7 Q# p8 i

0 `. l1 `3 s* m, Z  ~  echo $s;6 q8 ?3 q8 C, |' z6 g2 `$ R
  preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出
5 k: m1 R1 c1 A/ \) P8 _9 J  $arr = get_td_array($table[0][0]);//執行函數1 N2 t$ x, q5 |
  print_r($arr);  p+ U; a' E4 B* \
?>
  ]! l1 e/ P# Z0 A[/mw_shl_code] 函數是將表格轉換為數組的函數,網上找的。。問題是: 2014-10-21_220010.jpg

; a: j  @% o+ V! k
( n" j" u2 J& e% `* t4 B+ R4 J. [, j

最佳答案

查看完整內容

樓主結賬! 人呢

點評

不好意思,这里大神多,就来这里问了,小弟php,正则,基本啥都不会,着这些代码还费了2个小时  發表於 2014-10-21 22:03
回復

使用道具 舉報

發表於 2014-10-21 21:56:42 | 顯示全部樓層
樓主結賬!                      人呢
7 H6 x: k( D- A  `
回復

使用道具 舉報

 樓主| 發表於 2014-10-21 22:14:25 | 顯示全部樓層
[mw_shl_code=php,true]<?php
' \7 i+ ?+ ~2 n: V& H8 X" h* q2 D1 @header('Content-Type:text/html;charset:Utf-8');$ ~  |  R$ P6 n
function get_td_array($table) {
) Q# ~8 i7 X. g# |. h# T( f        $table = preg_replace("'<table[^>]*?>'si","",$table);
& M- \( `$ r" B        $table = preg_replace("'<tr[^>]*?>'si","",$table);* e1 \6 i4 h2 f6 X
        $table = preg_replace("'<td[^>]*?>'si","",$table);
% D& _7 d2 i# ?1 m; W, `        $table = str_replace("</tr>","{tr}",$table);0 K: K% u& J) D0 \) H* O
        $table = str_replace("</td>","{td}",$table);2 Y- i& g& c' d6 p3 S! F
        //去掉 HTML 標記
2 W0 [6 b2 T* E  M3 m* f        $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);3 m" h5 W$ z1 y- q6 Y; n
        //去掉空白字符
" \! ~# T- m3 D2 Q2 ~1 }        $table = preg_replace("'([rn])+'","",$table);  k+ g/ i1 c7 \% y
        $table = preg_replace('/ /',"",$table);& c1 |' y: U" t; N, r3 q7 x+ k! b9 F
        $table = str_replace(" ","",$table);0 ]) W9 H2 @: Z$ U& `
        $table = str_replace(" ","",$table);0 R1 ^/ x7 N* j& s
        $table = explode('{tr}', $table);
9 u/ n2 N4 M4 s1 T6 `# s        array_pop($table);* x" Y" C; k2 c+ c# Q$ J
        foreach ($table as $key=>$tr) {
: q7 M  e' z5 Q% `8 Z                $td = explode('{td}', $tr);
0 s% E4 {/ L- p! ]5 Q5 z                array_pop($td);
% O$ l% O) l) j; ^" ]                $td_array[] = $td;
7 }3 c, l% c2 N) j4 O0 j" ]        }
- X3 ]1 w, Z- P) h) |9 O6 h1 w& }" E        return $td_array;
+ p7 v+ R8 L/ d4 K' M- J0 _}
7 C' D+ H8 [4 I# ?% x2 r/ x8 H  //下載HTML網頁
: D6 S3 _- `9 \8 X; L  $s=file_get_contents('http://222.88.107.92/exam/query/query_detail.jsp?examid=2014102014&paperid=100094');! V/ q; Y* f2 U
  $s=mb_convert_encoding("$s", "UTF-8", "GBK");  //編碼轉換
2 }* u/ E3 _* H% j+ z0 V+ Y  //獲取最後一個table內容1 \  S6 O) y% z
  $s=substr($s,strrpos($s,'<table'));6 F; K0 C1 f7 v/ Z8 ?
  $s=substr($s,0,strpos($s,'</table>')+8);& ^2 o5 q7 @8 }! J
  //刪除超鏈接
" R' R7 f% Q  X! n6 [  $s=preg_replace('|<a href.*?</a>|','',$s);7 c0 y- D6 q# X4 }% b
  $s=preg_replace('|<td align="center" width="120">圖片</td>|','',$s);
' h0 W5 M1 l3 T  $s=preg_replace('|<td align=\'center\'></td>|','',$s);
* U  j) w( n  V6 |  i( t! o  $s=preg_replace('|<td align="center" width="120">評閱老師</td>|','',$s);
# `# G$ q1 I2 U3 D  $s=preg_replace('|<td align=\'center\'>110(.*?)<br></td>|','',$s);
' I+ P+ _5 ?, s; p  echo $s;
5 d1 U. e) x$ x# B  preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出. b7 `& q- k0 }' v
  $arr = get_td_array($table[0][0]);//執行函數. j; |9 v0 I7 I, j( E# ~) ]
  print_r($arr);
* C9 V  [/ I+ \1 i6 b4 i! O?>7 t5 ?% S  ]' V! p. s
[/mw_shl_code] 2014-10-21_221318.jpg
% s) M/ F9 F: L) Z; K
回復

使用道具 舉報

 樓主| 發表於 2014-10-21 22:15:00 | 顯示全部樓層
一樣的代碼,上面的這個就輸出了完全的數組: H! k! W; q. C$ x0 n: X% s
回復

使用道具 舉報

發表於 2014-10-22 13:52:01 | 顯示全部樓層
[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

評分

參與人數 1點點 +13 收起 理由
月光飞燕 + 13 認眞參與

查看全部評分

回復

使用道具 舉報

發表於 2014-10-22 13:53:15 | 顯示全部樓層
記得給懸賞!  20分啊,口水流出來了* l0 c& s; l0 b

點評

好的,终于来人了啊,我测试看看  發表於 2014-10-22 14:00
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 免费注册

本版積分規則

點基跨境 數位編輯創業論壇

GMT+8, 2025-7-15 05:04

By DZ X3.5

小黑屋

快速回復 返回頂部 返回列表