过期域名预定抢注

 找回密碼
 免费注册

求大神。。。

[複製鏈接]
發表於 2014-10-21 21:56:41 | 顯示全部樓層 |閱讀模式
懸賞20點點已解決
本帖最後由 xx19941215 於 2014-10-21 22:02 編輯 2 s+ [, _6 _; x2 h8 N- o

) N0 H  |8 }) a代碼[mw_shl_code=php,true]<?php
2 O  B; d( {! g, G+ X. K) T& ufunction get_td_array($table) {# n4 S' x2 _  T6 \) P1 E# d* k* ^5 E/ W
        $table = preg_replace("'<table[^>]*?>'si","",$table);% i5 a2 d5 W) @: g) E# G- x: U& Z) @
        $table = preg_replace("'<tr[^>]*?>'si","",$table);  Q$ K. i  o4 ^3 y9 u# k1 U" m
        $table = preg_replace("'<td[^>]*?>'si","",$table);
4 h4 E% q, P# ~/ ~        $table = str_replace("</tr>","{tr}",$table);
$ i1 E9 O. W/ s. l( g5 }* _  }$ O        $table = str_replace("</td>","{td}",$table);: G0 b; W4 w9 q9 `! P% @; T* Y
        //去掉 HTML 標記
" N( D- Q# F5 W2 c        $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
" @; ]3 L4 E' X8 u, e1 P7 b        //去掉空白字符' z% |; a7 H! G% j  T4 }! s4 I
        $table = preg_replace("'([rn])+'","",$table);
( m: z/ d% o8 c3 F. n) h        $table = preg_replace('/ /',"",$table);
7 H; o$ i' R# H        $table = str_replace(" ","",$table);
& z- s# ]1 Q& u5 f        $table = str_replace(" ","",$table);
. s. s& D/ N  H+ Y3 i        $table = explode('{tr}', $table);/ e+ ~( a- T* v( y
        array_pop($table);
; k2 p' w, ~$ m5 A, y; ~# Y7 o        foreach ($table as $key=>$tr) {' L0 ^, U6 n" {, P4 h- N
                $td = explode('{td}', $tr);
; X9 T5 f! B1 Z                array_pop($td);
4 y+ k6 J# _4 P  P- x                $td_array[] = $td;
6 @( x; E: q* V- x" `5 ?3 D, a9 H        }) g+ U9 C( m6 l( I. V
        return $td_array;; J% b* _# L# T  `- R
}
7 t# ~* q. ?. ]  //下載HTML網頁
* O0 G! E9 k$ O6 F# B9 q( U  $s=file_get_contents('http://chengji.lideping.com:7000/exam/query/query.jsp?logname=20133011235');6 R: w6 t. W& U3 |% P% k/ n8 o
  $s=mb_convert_encoding("$s", "UTF-8", "GBK");  //編碼轉換
1 F* {; z! N+ V/ ^! G* F& ~  //獲取最後一個table內容
/ n; H3 Q( X7 e  $s=substr($s,strrpos($s,'<table'));# |% k( j. z9 V' D
  $s=substr($s,0,strpos($s,'</table>')+8);: b" _) g1 @0 _7 l# I4 K' @
  //刪除超鏈接
* e: T5 f* M4 a  $s=preg_replace('|<a href.*?</a>|','',$s);
/ c8 _# b0 b/ o+ d7 @, g$s=preg_replace('|<td align=\'center\'></td>|','',$s);//刪除多餘空格
* [' f* x. v% V* m# V+ X) M1 h; Q- U3 E' c& a! d
  echo $s;  j( k' `" H) k' E- _9 f
  preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出
& {& \. U: W6 x8 ~  $arr = get_td_array($table[0][0]);//執行函數7 K' y; Y" Z- i. x# A
  print_r($arr);
2 p3 K6 O% H& |( @( s/ r3 [?>
. T1 J, `# s9 T% ][/mw_shl_code] 函數是將表格轉換為數組的函數,網上找的。。問題是: 2014-10-21_220010.jpg
- p- H7 c' g$ g3 Y3 {, q* t0 K

- l  E/ d7 v# `

最佳答案

查看完整內容

樓主結賬! 人呢

點評

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

使用道具 舉報

發表於 2014-10-21 21:56:42 | 顯示全部樓層
樓主結賬!                      人呢! `  R4 ]) x8 A. X9 S4 ~- ^5 i
回復

使用道具 舉報

 樓主| 發表於 2014-10-21 22:14:25 | 顯示全部樓層
[mw_shl_code=php,true]<?php
7 L/ ~; f2 l! \5 C, Z0 Z( G" I7 {header('Content-Type:text/html;charset:Utf-8');
% j/ z- {( @( r6 l) ~function get_td_array($table) {
# Z% E  Q' `- o/ w( w2 E/ @        $table = preg_replace("'<table[^>]*?>'si","",$table);. O; I/ ^% f/ c! t6 W/ x- C
        $table = preg_replace("'<tr[^>]*?>'si","",$table);6 q) O( h& d! D$ s9 H1 ], T; _& S
        $table = preg_replace("'<td[^>]*?>'si","",$table);
. _' X5 F' I' e( g2 M        $table = str_replace("</tr>","{tr}",$table);3 W- U9 i( V1 D: ^4 v$ V1 a
        $table = str_replace("</td>","{td}",$table);
, C/ m+ x( C+ u5 ~! Z        //去掉 HTML 標記: u9 ?  c- u: _2 \. Z
        $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);; Z7 A: s5 d9 N2 k
        //去掉空白字符
( R5 R1 V% O# N: Y, O; e' _: ^        $table = preg_replace("'([rn])+'","",$table);
6 j- m0 w$ G5 D3 k$ {        $table = preg_replace('/ /',"",$table);
8 v  [5 ]/ a# ~) n        $table = str_replace(" ","",$table);0 m, B' z* V! ]
        $table = str_replace(" ","",$table);' k5 @0 e* H1 n3 ]* b6 m1 T! Y
        $table = explode('{tr}', $table);
" d  D5 G3 L% z! `        array_pop($table);0 p& ~: B' T  ^
        foreach ($table as $key=>$tr) {
# [: N/ p- l% c  ]! p2 B                $td = explode('{td}', $tr);
( i& J. y0 I% ?2 j  S& m% m# h                array_pop($td);
' i# v( c) q4 U# e6 g1 G4 F                $td_array[] = $td;
/ x) Q7 x2 s! h, ?        }
/ o: ~5 O7 R5 B/ V        return $td_array;
3 O' ]8 V& a# a}
! R! ], X1 B; R6 R* _1 {  //下載HTML網頁6 `7 [9 P/ N" @" o
  $s=file_get_contents('http://222.88.107.92/exam/query/query_detail.jsp?examid=2014102014&paperid=100094');1 j% r- g" G+ k$ S# C7 R- c- M: C8 b  C
  $s=mb_convert_encoding("$s", "UTF-8", "GBK");  //編碼轉換
7 l$ W4 h' h0 R3 V- e  //獲取最後一個table內容
" a9 [! E& W, Z9 z% Q) @3 v6 {  $s=substr($s,strrpos($s,'<table'));( A3 F! k9 w. O% i/ J8 {( T6 O# I
  $s=substr($s,0,strpos($s,'</table>')+8);
" m- b+ Q% {5 o; Y  //刪除超鏈接6 p% n/ F1 {9 H- J
  $s=preg_replace('|<a href.*?</a>|','',$s);8 V: f. c  y; |) K7 M# H) U
  $s=preg_replace('|<td align="center" width="120">圖片</td>|','',$s);
' ], ], y2 N8 y/ P1 t2 |  j  $s=preg_replace('|<td align=\'center\'></td>|','',$s);  G# Q% N' C- e4 V" i/ E2 m
  $s=preg_replace('|<td align="center" width="120">評閱老師</td>|','',$s);
4 Q, e0 V: G' c2 F- o  $s=preg_replace('|<td align=\'center\'>110(.*?)<br></td>|','',$s);
  D& v, [7 [- T: f3 y  Z# K+ g  echo $s;( F7 s" z6 f$ ~! L
  preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出
7 r! N2 L. x6 v1 c6 g" T  $arr = get_td_array($table[0][0]);//執行函數
* Q' P* v5 l* r  print_r($arr);  B& d3 T) e+ ~# g/ A
?>
8 K% o0 J# v4 ^5 U" J[/mw_shl_code] 2014-10-21_221318.jpg " X+ J& g8 W6 c* V
回復

使用道具 舉報

 樓主| 發表於 2014-10-21 22:15:00 | 顯示全部樓層
一樣的代碼,上面的這個就輸出了完全的數組
- v6 O# D% ]: \% y% w  h$ J+ @) J- c
回復

使用道具 舉報

發表於 2014-10-22 13:52:01 | 顯示全部樓層
[mw_shl_code=php,true]<!DOCTYPE html>
+ z: `( h: ?; y% F& D<html lang="zh-cn">9 @* D- g4 y+ s7 G
<head>
# {6 V# ~1 r: J  @' |+ k<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />8 Z& k' g4 l% ~# K& l, k
<title>測試</title>2 Y9 m  a/ R+ C# X3 P6 Y! d1 _
</head>' [( `; d# [+ y+ L# H
<body>' x# X  W3 O# Q3 F! ~6 {
<?php' S; e8 k7 n% D! L
function get_td_array($table) {0 U5 L5 a7 i8 c" I( E% ]: C! C
        //去掉table標籤上的屬性% B. w! p6 Q" N' @6 @7 t/ `% @4 w
        $table = preg_replace("'<table[^>]*?>'si","",$table);$ A" n* F$ k4 y* }9 Y
        //去掉TR上的屬性' g' e/ k- p% o. T9 n1 D
        $table = preg_replace("'<tr[^>]*?>'si","",$table);( J  f( p; F9 _# V5 ?
        //去掉td上的屬性8 a2 R/ `# P/ D7 z$ {$ l$ T
        $table = preg_replace("'<td[^>]*?>'si","",$table);: _5 S0 T  o1 h
        //標籤替換
  s* s* I; N/ k) [& d6 R% H, l. G        $table = str_replace("</tr>","{tr}",$table);
4 u: n& d( q  O% K; `! S( P6 Y        $table = str_replace("</td>","{td}",$table);
* T7 ^% h, B+ s3 E# _7 H" k& {9 n8 v: r  S# K' H% I8 g, H' K
        //去掉 HTML 標記
* w1 X; V" ?& ~5 _# x( z        $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);0 U4 W4 O  i) b) Q5 j. h! m% s
        //去掉空白字符
  j% U, G/ d- s) v, t2 Y, [        $table = preg_replace("'([rn])+'","",$table);
, l& L- G* d$ w. n3 n% C7 }/ `) k        $table = preg_replace('/ /',"",$table);
  G, F2 S" x* q) s- J        $table = str_replace(" ","",$table);6 m, o: D( g7 s& N; f
        $table = str_replace(" ","",$table);
/ s% y8 J4 }6 {* a: N7 J" {1 R        $table = explode('{tr}', $table);
/ k0 L: m1 O# Y2 L8 [- f1 s% A" `- x3 }1 d
        //這裡打印出來是長度為2的數組。
! ?8 `" ]4 L9 Y3 M        print_r($table);
$ a% s# D8 ^4 q: l  {8 a        //這句代碼有問題,這個會把數組的第二個元素刪掉,所以得不到想要的結果
. y8 `  W8 D" W0 ^1 u1 `5 e( v        //array_pop($table);4 ~+ ^6 O" N% [, K
        1 C, t6 m6 f. e! v; e
        $td_array = array();8 B9 p0 ]3 S( B% m1 l" X

  g# x% t! Y+ v1 T5 J; T        foreach ($table as $key=>$tr) {
3 U3 s+ f7 A! E, G) d                $td = explode('{td}', $tr);4 s( \6 b, }6 u
                array_pop($td);
; X  u  c7 T& d0 F/ z7 u: Z2 [$ H6 Q( n                $td_array[] = $td;
7 i5 J) I1 b5 n7 l        }
9 _; g# u$ a6 H- y' x9 C        return $td_array;: C2 f% q- F: z/ y+ b& ?
}
( z+ W7 }' n0 h2 t) I2 I) F5 s//下載HTML網頁
# o+ k! g5 T+ X, {3 ~2 M% h$s=file_get_contents('http://chengji.lideping.com:7000/exam/query/query.jsp?logname=20133011235');
* `' X9 c/ {# @$s=mb_convert_encoding("$s", "UTF-8", "GBK");  //編碼轉換5 x$ ], G5 C: B* R+ U- X2 V+ K
//獲取最後一個table內容7 U9 f! T% j6 T
$s=substr($s,strrpos($s,'<table'));1 h" R) ^2 \, H; A
$s=substr($s,0,strpos($s,'</table>')+8);& H; I9 |5 t/ K. k
//刪除超鏈接
( d( V; b5 u2 C: W/ B9 T$s=preg_replace('|<a href.*?</a>|','',$s);
& \0 o$ @: h# h2 _$s=preg_replace('|<td align=\'center\'></td>|','',$s);//刪除多餘空格$ `3 w* l7 U: w& m5 J8 Y* a

2 q! B- e0 h2 V5 K1 n5 Aecho $s;
  w3 n, S% y+ ]+ d7 C4 kpreg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出# n6 W' x) m$ B! W/ L

. W& H! z4 b5 q$ W0 r$arr = get_td_array($table[0][0]);//執行函數
9 Z5 K% L4 b! A9 |( A" [( @print_r($arr);4 E+ M0 A7 A  Q# G# ^
?>8 o# Z, n1 x! \3 F% J' T1 Q; J
</body>9 h* o7 ~8 L' ]# k0 c
</html>
, R- C  ~6 B- q4 \0 t, Q) [( [[/mw_shl_code]+ h( _! A% p& c

. L% s( R- v- c1 j6 n% a已調整完全正確輸出,給樓主你註釋了哪句代碼的問題. {: Y; u9 y9 {$ R3 q$ J

評分

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

查看全部評分

回復

使用道具 舉報

發表於 2014-10-22 13:53:15 | 顯示全部樓層
記得給懸賞!  20分啊,口水流出來了
1 R) g2 s- e5 j8 z2 {: e9 A

點評

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

使用道具 舉報

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

本版積分規則

过期高净值品牌域名预定抢注

點基

GMT+8, 2025-12-17 14:33

By DZ X3.5

小黑屋

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