过期域名预定抢注

 找回密碼
 免费注册

求大神。。。

[複製鏈接]
發表於 2014-10-21 21:56:41 | 顯示全部樓層 |閱讀模式
懸賞20點點已解決
本帖最後由 xx19941215 於 2014-10-21 22:02 編輯
# U, a- I* Q4 }& S
, ]  ?& P- X8 j' }代碼[mw_shl_code=php,true]<?php
* k' S8 w: Y* e6 i* Gfunction get_td_array($table) {2 S1 j" ~8 g/ b) o/ |: X
        $table = preg_replace("'<table[^>]*?>'si","",$table);* G! i8 ]) v2 [; h% r
        $table = preg_replace("'<tr[^>]*?>'si","",$table);9 g& {3 G! B4 K$ g
        $table = preg_replace("'<td[^>]*?>'si","",$table);* K8 \% u* m- n( L# U+ Y6 C  M. p
        $table = str_replace("</tr>","{tr}",$table);
5 F8 }( l1 s4 D. N5 ]% {        $table = str_replace("</td>","{td}",$table);# W6 L( E( J) v0 W" `
        //去掉 HTML 標記
2 }9 A4 J8 W" M8 l6 c* x3 z7 V4 L* Y        $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
5 C# g, n; U4 U( f        //去掉空白字符
8 M3 P# @- @  d  U6 y2 o: y        $table = preg_replace("'([rn])+'","",$table);* F! L9 a' @+ i" R5 C+ d2 W% t
        $table = preg_replace('/ /',"",$table);% l# q* U& h& e" X' Z
        $table = str_replace(" ","",$table);
/ [3 G+ `+ x  t, p8 {9 ?        $table = str_replace(" ","",$table);  D) k* }0 }. l, q
        $table = explode('{tr}', $table);* L8 m* B5 W/ y
        array_pop($table);
% t# r6 @/ b2 n0 l9 G. h        foreach ($table as $key=>$tr) {
1 d+ x( @$ Z! D4 }. C                $td = explode('{td}', $tr);. I& Y% f5 y" m) E
                array_pop($td);4 l  R  W4 l7 P9 U8 c3 P2 G; l
                $td_array[] = $td;3 Z. g) C" G3 w+ Y8 S# r2 E5 v
        }, {/ y, ]. J: L% T( K
        return $td_array;
; i. x4 x1 I7 I6 t# P+ h) R} : Z+ t$ g/ A+ G- s# z+ x
  //下載HTML網頁
  T! U5 `& x" w' S  $s=file_get_contents('http://chengji.lideping.com:7000/exam/query/query.jsp?logname=20133011235');
3 @' n9 G6 x0 N4 e" b5 N  $s=mb_convert_encoding("$s", "UTF-8", "GBK");  //編碼轉換: d4 ^7 e3 H& P% [& D' I
  //獲取最後一個table內容
3 H6 b' C, R2 ?( Z$ F8 r  $s=substr($s,strrpos($s,'<table'));
. j, Q6 |. y; v$ Q; s# ~# z. X; V  $s=substr($s,0,strpos($s,'</table>')+8);  a- k* j* \# f
  //刪除超鏈接) J4 v3 a; F+ ^6 {. ?8 [
  $s=preg_replace('|<a href.*?</a>|','',$s);
7 I) p- ^: y7 G3 T) B$ G" E% A$s=preg_replace('|<td align=\'center\'></td>|','',$s);//刪除多餘空格
  [% l1 F1 H9 O6 o$ a4 a4 n9 s% h% P2 f# ~4 _) V
  echo $s;
  @+ f; e/ g. N, v) m  preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出
4 e8 i& Q# X+ x6 V( e( ]2 w  $arr = get_td_array($table[0][0]);//執行函數; i1 ^; ]& T- l/ g( V  T
  print_r($arr);
+ g$ h7 ?: _) j4 X3 J?>
1 O& h7 }. C  c& N% T[/mw_shl_code] 函數是將表格轉換為數組的函數,網上找的。。問題是: 2014-10-21_220010.jpg
: Y, P7 t5 Q5 v- V

3 y& x, f$ _% A

最佳答案

查看完整內容

樓主結賬! 人呢

點評

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

使用道具 舉報

發表於 2014-10-21 21:56:42 | 顯示全部樓層
樓主結賬!                      人呢
" C. e' h( d' G
回復

使用道具 舉報

 樓主| 發表於 2014-10-21 22:14:25 | 顯示全部樓層
[mw_shl_code=php,true]<?php) \' Y' t0 b6 F: ]& }; R
header('Content-Type:text/html;charset:Utf-8');3 o9 a2 A+ }8 v4 j: ]: b" ^
function get_td_array($table) {% O  S, m2 \0 `
        $table = preg_replace("'<table[^>]*?>'si","",$table);
+ k* N6 V+ W% W7 w        $table = preg_replace("'<tr[^>]*?>'si","",$table);
0 p( u: l( d5 \1 V/ e        $table = preg_replace("'<td[^>]*?>'si","",$table);
& o7 V% _2 Y9 Q4 F        $table = str_replace("</tr>","{tr}",$table);
6 w- P% Z5 B1 w  @7 \4 y% O1 @! _        $table = str_replace("</td>","{td}",$table);6 u4 O# B) x# K- v! x; s6 b0 A
        //去掉 HTML 標記. V6 h/ X. T1 t3 T# S
        $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
2 _! @) I$ Z# ]( [+ _        //去掉空白字符/ |9 \% c: ^8 e% s+ T% d
        $table = preg_replace("'([rn])+'","",$table);3 x& F4 ]& b- e
        $table = preg_replace('/ /',"",$table);8 E. z7 t- y9 P# h
        $table = str_replace(" ","",$table);' Y/ j  `: u* Y! P
        $table = str_replace(" ","",$table);
4 z) v( [+ y8 `$ ~        $table = explode('{tr}', $table);
' y' G/ ]; J5 E        array_pop($table);
5 O6 m5 ^1 S9 T2 P2 M        foreach ($table as $key=>$tr) {
5 W5 q; x8 Q' U, C4 Z/ w" a3 b                $td = explode('{td}', $tr);
0 g1 Q9 G' ^" {4 L% }' x. o9 L                array_pop($td);" ~. ]# ?) q; K) Z- j& L2 h
                $td_array[] = $td;
! L% d+ a- M. X; ?        }6 v$ w7 U  l' l
        return $td_array;
& M1 `% m3 H+ A( B; x} * i3 t( v  G' L5 v/ C3 S9 T
  //下載HTML網頁
+ P6 }6 d3 |7 \  $s=file_get_contents('http://222.88.107.92/exam/query/query_detail.jsp?examid=2014102014&paperid=100094');0 x! `3 Z$ s' [6 W. J0 Z' a0 ~
  $s=mb_convert_encoding("$s", "UTF-8", "GBK");  //編碼轉換
/ ?/ n' V4 z; H: C6 O0 N  //獲取最後一個table內容
& ?; Y" O7 ?6 J+ j" B) A) R  $s=substr($s,strrpos($s,'<table'));# }8 ~/ d; v$ [; m
  $s=substr($s,0,strpos($s,'</table>')+8);2 B% t$ z: ?# P1 y
  //刪除超鏈接
5 _, a6 ?/ e8 h; l: @& X  $s=preg_replace('|<a href.*?</a>|','',$s);
7 r8 {: q7 p' F' X$ S1 O4 J4 ?  $s=preg_replace('|<td align="center" width="120">圖片</td>|','',$s);
5 g) g5 Q* A! k- ]' [" Z  $s=preg_replace('|<td align=\'center\'></td>|','',$s);1 P. A# W6 z) S. s; |: I. d/ f& W
  $s=preg_replace('|<td align="center" width="120">評閱老師</td>|','',$s);! i! u' Y. m# ?5 T# b1 ^2 H5 T& B
  $s=preg_replace('|<td align=\'center\'>110(.*?)<br></td>|','',$s);$ {  D& N# D: _; n. a" B( n) i
  echo $s;
; @+ v/ y! O. Q1 k  preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出9 c  Y# V# f" w
  $arr = get_td_array($table[0][0]);//執行函數! ^# B( i& v4 v5 w1 h
  print_r($arr);
3 d3 R5 z: K( @; v: m/ W6 R?>- X- y( [+ C' v% l0 ]) u  O0 r* R5 S
[/mw_shl_code] 2014-10-21_221318.jpg
' p. w+ Y8 {4 A
回復

使用道具 舉報

 樓主| 發表於 2014-10-21 22:15:00 | 顯示全部樓層
一樣的代碼,上面的這個就輸出了完全的數組( Y# J1 y5 i% |+ k8 I
回復

使用道具 舉報

發表於 2014-10-22 13:52:01 | 顯示全部樓層
[mw_shl_code=php,true]<!DOCTYPE html>
, s9 x# K; f/ ]8 P<html lang="zh-cn">
2 Q* ~( m- s# X5 W<head>
9 _( D- Q+ |4 t<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9 E  y% U! ]3 ~4 s$ N2 N* x<title>測試</title>( m# j9 F7 ?# W: ]9 e
</head>: V7 h; `8 o. u% |. q& q
<body>; O; I+ p4 I$ F. _  f0 P
<?php
/ @& X) P( ?. L) K6 f! u7 k  Yfunction get_td_array($table) {
+ O- R' X1 O2 {- i  Y        //去掉table標籤上的屬性
9 U6 `+ Y) R. Y8 H) h# E+ O        $table = preg_replace("'<table[^>]*?>'si","",$table);; V( O, L$ `2 }# X, u1 S9 @  T
        //去掉TR上的屬性
* B3 o5 m" S- Z, j9 [! ~+ e/ U0 i        $table = preg_replace("'<tr[^>]*?>'si","",$table);
& R' S' S2 O$ e* |( j        //去掉td上的屬性0 b% q* m# u  X8 B
        $table = preg_replace("'<td[^>]*?>'si","",$table);
7 {* C( B& T# Z- h        //標籤替換% C! {8 g' T- B- X
        $table = str_replace("</tr>","{tr}",$table);
  {- B  z; P8 w. g7 s0 t        $table = str_replace("</td>","{td}",$table);
9 f7 ?( f+ N$ i" {0 u6 d1 W
& ^5 M' k+ ~6 K; T8 m        //去掉 HTML 標記. q: c/ m# U; b( G
        $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);) q8 P5 u$ w! b$ F9 N$ g
        //去掉空白字符! d- q8 l/ r# E% U4 c5 t/ E
        $table = preg_replace("'([rn])+'","",$table);- B( A# V8 s# Y  T3 f9 u
        $table = preg_replace('/ /',"",$table);
+ S" ~9 L6 E& W. i        $table = str_replace(" ","",$table);
( z8 ], A3 `  q1 Z5 Y4 r        $table = str_replace(" ","",$table);, A# P- R9 {+ z
        $table = explode('{tr}', $table);0 r$ d- X9 r5 a- H4 o
# T6 B9 X) I  g8 ^
        //這裡打印出來是長度為2的數組。/ G+ `' d1 e8 C+ V! ?3 G# R: ^
        print_r($table);" ]0 J. a4 p5 x+ E; Y& s8 @; e
        //這句代碼有問題,這個會把數組的第二個元素刪掉,所以得不到想要的結果" V; T. h6 _# S2 A' n2 V
        //array_pop($table);
- I: H  u. |  a' d3 J       
4 A3 e! k! F6 ^6 s+ h2 m4 A/ O        $td_array = array();9 N( ?0 E7 |) b4 Z6 }3 H

) Q# V( w1 o- r7 z/ q        foreach ($table as $key=>$tr) {
: x$ q0 W2 V2 o3 C7 l0 G0 e                $td = explode('{td}', $tr);
3 L" K" j: f9 b* @7 d% K- X0 x                array_pop($td);" |8 g' H$ N/ Z, a0 ?
                $td_array[] = $td;
5 {0 O0 k. P, _" o        }9 ?: h5 N. k5 ?) `
        return $td_array;
. x: i) J, Z. E! j6 A9 S- O}  o; n2 T, S! P7 O1 k
//下載HTML網頁
+ Y& @4 E# E, B. Z5 j' \( V8 Q  b$s=file_get_contents('http://chengji.lideping.com:7000/exam/query/query.jsp?logname=20133011235');# u! y/ H% ?+ K8 X% f  ]6 P
$s=mb_convert_encoding("$s", "UTF-8", "GBK");  //編碼轉換
/ F3 C8 y! \6 D6 O9 k//獲取最後一個table內容8 a! d! n* v) i
$s=substr($s,strrpos($s,'<table'));7 E" E0 V1 v  B( x* N7 U' n: E
$s=substr($s,0,strpos($s,'</table>')+8);
$ U8 ?8 C# z+ I6 N//刪除超鏈接
3 _+ ]5 ?$ A7 @0 ~5 a1 J9 J/ M$s=preg_replace('|<a href.*?</a>|','',$s);
1 g( p1 _- v5 y! Y) R  F$s=preg_replace('|<td align=\'center\'></td>|','',$s);//刪除多餘空格
" y* N8 S+ L0 [* T( |: p& ?0 Y/ Z5 h% O. [( y
echo $s;
8 y2 q% I# X$ B6 zpreg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出# G$ W& K: Z- o1 u- j

& q9 ~: f- U" ~, r" R$arr = get_td_array($table[0][0]);//執行函數# t* m# `$ P2 s& m. W" S
print_r($arr);
2 s) M  {6 ~% x) C?>
) g7 c, ]' ?9 y9 a3 O; C</body>
8 p& w/ V; `, f, v7 H3 Y5 l</html>/ [  D4 i* `5 a! `
[/mw_shl_code]9 y9 \6 S" V1 N3 w/ ]# S: V
; p( N: J8 {# \0 S
已調整完全正確輸出,給樓主你註釋了哪句代碼的問題8 N2 O: B7 C. u6 N

評分

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

查看全部評分

回復

使用道具 舉報

發表於 2014-10-22 13:53:15 | 顯示全部樓層
記得給懸賞!  20分啊,口水流出來了
" c8 @* R/ I& K

點評

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

使用道具 舉報

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

本版積分規則

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

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

GMT+8, 2025-7-15 09:32

By DZ X3.5

小黑屋

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