过期域名预定抢注

 找回密碼
 免费注册

求大神。。。

[複製鏈接]
發表於 2014-10-21 21:56:41 | 顯示全部樓層 |閱讀模式
懸賞20點點已解決
本帖最後由 xx19941215 於 2014-10-21 22:02 編輯
" G/ F  b2 H& d8 g# x% f# a4 R
% W' k+ P' p2 u代碼[mw_shl_code=php,true]<?php1 u! p# a, h; O& b5 l, J
function get_td_array($table) {
' P) P+ l* l2 ~( w        $table = preg_replace("'<table[^>]*?>'si","",$table);6 c1 M! O: h3 E
        $table = preg_replace("'<tr[^>]*?>'si","",$table);
: z. ~& L: `  b. T' g4 C/ z        $table = preg_replace("'<td[^>]*?>'si","",$table);% p$ `+ U/ q# s; Y* M" _) |% o
        $table = str_replace("</tr>","{tr}",$table);
" W9 r, @: ^7 b3 u        $table = str_replace("</td>","{td}",$table);
$ t7 N1 R/ q3 S/ `( z* ]% M$ V) l        //去掉 HTML 標記
8 n, q; y/ P5 T+ k: R7 `        $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
! X, d. j# a- y  U2 Z  L% @" M        //去掉空白字符( Z5 z2 W" g9 |4 ^
        $table = preg_replace("'([rn])+'","",$table);
/ p' j- s  C0 d. r& Z        $table = preg_replace('/ /',"",$table);
5 v$ o4 m$ c' X! k( K$ U( c3 T        $table = str_replace(" ","",$table);
( @) D: j6 h  d        $table = str_replace(" ","",$table);
, q* |: _5 g6 l# o+ X% }        $table = explode('{tr}', $table);
, C# [) u& H$ w& K8 S        array_pop($table);
( b; \1 e7 W& b- Q# }        foreach ($table as $key=>$tr) {) y8 w% Q2 [9 n3 y6 W& j5 X
                $td = explode('{td}', $tr);
( |& E0 v$ Z! Q2 W                array_pop($td);
# }- c, [4 P: i9 i: H6 |, P3 P                $td_array[] = $td;6 C0 o  T2 J) K3 H" K' @* ~
        }  C* x$ v' W8 O. V
        return $td_array;: U6 r( x  M/ E0 w4 i
}
4 d: M8 k# `6 U' S: Q# I% a8 I3 }  //下載HTML網頁  c- T7 q) G3 S+ {$ U5 H3 R2 g4 y
  $s=file_get_contents('http://chengji.lideping.com:7000/exam/query/query.jsp?logname=20133011235');
8 o- L0 x3 ]+ \: S* Y  $s=mb_convert_encoding("$s", "UTF-8", "GBK");  //編碼轉換7 M* U7 `- X) l9 M- a: j( u, u. _
  //獲取最後一個table內容4 }" j  ~8 K( ~7 E
  $s=substr($s,strrpos($s,'<table'));
/ q' P8 s9 B5 \3 I% ]  $s=substr($s,0,strpos($s,'</table>')+8);
6 r+ k: [: r! U- n' \  d  //刪除超鏈接
7 I, p& u( r8 E" j# j# u. P  $s=preg_replace('|<a href.*?</a>|','',$s);2 M: J" `8 F. c1 l$ w3 @
$s=preg_replace('|<td align=\'center\'></td>|','',$s);//刪除多餘空格
* s. ?. {+ m  X" i9 t3 ]' z% F. \) L3 [* D+ L# |
  echo $s;
4 g& K& q  c3 Y1 h9 d( }8 {6 Y  preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出
" |5 s7 V9 Z+ s" f- Y7 h2 p  $arr = get_td_array($table[0][0]);//執行函數
8 L: X2 ?/ N& E' X  print_r($arr);
3 i2 d$ B8 U" Q  y* B- Y?>2 ^# I# b1 J. Z: I: J% M1 x
[/mw_shl_code] 函數是將表格轉換為數組的函數,網上找的。。問題是: 2014-10-21_220010.jpg

& H* `3 O( u5 @1 C& @" o' I5 [  n3 `& @) w! E

最佳答案

查看完整內容

樓主結賬! 人呢

點評

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

使用道具 舉報

發表於 2014-10-21 21:56:42 | 顯示全部樓層
樓主結賬!                      人呢
9 Q3 ~3 H$ X3 Y1 i  `- [$ \
回復

使用道具 舉報

 樓主| 發表於 2014-10-21 22:14:25 | 顯示全部樓層
[mw_shl_code=php,true]<?php
& W7 e9 L9 h8 b" ?& M/ m. X* ?+ uheader('Content-Type:text/html;charset:Utf-8');
8 F' q! X$ L3 ?# _" x3 z1 zfunction get_td_array($table) {
* j& ]* I- Z1 T" u. K2 o        $table = preg_replace("'<table[^>]*?>'si","",$table);
; K- x- E2 I3 }* z% ~$ T/ k2 r        $table = preg_replace("'<tr[^>]*?>'si","",$table);1 y$ t. B( Y, k2 W* m
        $table = preg_replace("'<td[^>]*?>'si","",$table);5 e3 d/ {# \& ]* |6 j3 A+ Q) S% f$ L
        $table = str_replace("</tr>","{tr}",$table);. M8 e* N6 Y$ k
        $table = str_replace("</td>","{td}",$table);
* F- c% W& x+ r# F        //去掉 HTML 標記! T  S) F5 _3 ]' q' o) v, \1 o
        $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);3 x6 E/ u: B5 _9 P2 \/ b  _
        //去掉空白字符; A; P9 t7 P- e$ ~0 g7 O
        $table = preg_replace("'([rn])+'","",$table);
4 r9 g6 T, M; {; [, W        $table = preg_replace('/ /',"",$table);
" ?' f2 o) i6 h6 I: w3 v) A. f        $table = str_replace(" ","",$table);
6 r& b7 U+ w0 a# R% X) q9 f- E8 J        $table = str_replace(" ","",$table);
& S, U7 O% V1 l. D6 i& y        $table = explode('{tr}', $table);7 V- g( l0 ^3 [( Q, @: f
        array_pop($table);8 H- d' |6 e' S/ b- ^1 A% Y8 Q8 I
        foreach ($table as $key=>$tr) {' t  t! N6 e7 p8 }
                $td = explode('{td}', $tr);4 P7 R' l5 X: k6 C# u
                array_pop($td);
  C9 o; H* T, `; Z/ w; n) C                $td_array[] = $td;
2 Q  ?( k, S; d& B( j+ f. [0 Z" [        }* c5 u. y, S8 O
        return $td_array;4 L: T0 P& X6 c& f
}
  T2 ^/ n- d8 d' }  //下載HTML網頁( s# Y8 B1 ]7 j) {2 [
  $s=file_get_contents('http://222.88.107.92/exam/query/query_detail.jsp?examid=2014102014&paperid=100094');# f* A! V) U+ {, d' {- x  \
  $s=mb_convert_encoding("$s", "UTF-8", "GBK");  //編碼轉換
% y) {5 t. v( X  //獲取最後一個table內容
* T+ F7 E. ~$ q. W! p  $s=substr($s,strrpos($s,'<table'));; ?- v* Q7 c& @  d
  $s=substr($s,0,strpos($s,'</table>')+8);$ r& I5 h* s7 h, q* R# I- Y/ I
  //刪除超鏈接
; A6 T. v) ?  d  N4 ~  $s=preg_replace('|<a href.*?</a>|','',$s);
" t+ n5 ~) ^% B  $s=preg_replace('|<td align="center" width="120">圖片</td>|','',$s);
' }* P+ @* l! ~: W9 {  $s=preg_replace('|<td align=\'center\'></td>|','',$s);" @8 g. N4 P  V8 V' O% K1 I6 }
  $s=preg_replace('|<td align="center" width="120">評閱老師</td>|','',$s);
1 o$ H. a5 M1 i+ O" X; T# x. F* [7 G$ }  $s=preg_replace('|<td align=\'center\'>110(.*?)<br></td>|','',$s);
/ |* m+ [% A% C  echo $s;0 Q; G. A% J9 n* `1 |  x
  preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出( r) y! D) ?8 n- h
  $arr = get_td_array($table[0][0]);//執行函數/ |; |, ]+ X: p: f" {3 U6 N( `
  print_r($arr);$ @9 r2 p4 |0 M! |% \
?>% U0 B" A4 A! ]- q! F
[/mw_shl_code] 2014-10-21_221318.jpg
2 ]; m' q! o6 T* B+ O- A9 g+ u
回復

使用道具 舉報

 樓主| 發表於 2014-10-21 22:15:00 | 顯示全部樓層
一樣的代碼,上面的這個就輸出了完全的數組
4 u. u& s' E( H0 k+ y' W4 q" p$ ~
回復

使用道具 舉報

發表於 2014-10-22 13:52:01 | 顯示全部樓層
[mw_shl_code=php,true]<!DOCTYPE html>0 R" p# j1 {9 x7 n: ~
<html lang="zh-cn">
  r! b4 Y: x. x) P  @; O  H( R0 A<head>+ s# D: e; J8 G% D
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
2 `$ y1 v/ j2 r# H<title>測試</title>) B  V/ d. ~8 d# m
</head>
4 G7 q; f) |- C5 r. i<body>' n/ T" s/ p8 t6 X9 l
<?php
3 A  [: X6 n8 Nfunction get_td_array($table) {. Y8 I2 B4 P4 c1 V! x+ M3 K
        //去掉table標籤上的屬性
/ @. z5 E) |# c/ e0 A) f        $table = preg_replace("'<table[^>]*?>'si","",$table);
/ h  N' P9 U/ C: O1 c& L% S        //去掉TR上的屬性
; Z. U* C& e& H- h( U        $table = preg_replace("'<tr[^>]*?>'si","",$table);
" S. J6 ?. k4 i3 q        //去掉td上的屬性% G1 y8 R" Z- c  O6 y
        $table = preg_replace("'<td[^>]*?>'si","",$table);% k4 p) {; g" b/ D+ c* E+ v* K
        //標籤替換
8 o3 P: K' [% U& Y        $table = str_replace("</tr>","{tr}",$table);# P, f! I( Z  H
        $table = str_replace("</td>","{td}",$table);' D# N. ~- D* K8 _1 H
  n% q; C5 m1 U/ a) T
        //去掉 HTML 標記9 Z  p( E% q8 f; [* h/ n, v
        $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
9 V. G3 ~1 Q* J4 H) T6 F        //去掉空白字符
" a3 x, Q# a$ B" j        $table = preg_replace("'([rn])+'","",$table);
  k* ~  r5 L- q* w0 }        $table = preg_replace('/ /',"",$table);
5 _$ h0 M/ c* p9 G: M8 I        $table = str_replace(" ","",$table);
; P5 \9 `# S* N# V( D! m* ^8 C* G        $table = str_replace(" ","",$table);; K; h/ i( g# M, [* }
        $table = explode('{tr}', $table);
) Q. O' N0 m  n7 I, V, U- v' R
+ ]( [: H5 V9 X9 F        //這裡打印出來是長度為2的數組。
- \' t/ o% G. ?0 x6 x" L) r$ }        print_r($table);
. S4 I1 |( e$ a6 s8 {        //這句代碼有問題,這個會把數組的第二個元素刪掉,所以得不到想要的結果) a* \! i! P9 M; ^6 ?( c1 {
        //array_pop($table);
, }2 L/ j, V$ S1 I) X3 g        / q6 k! `4 H& A( o8 a
        $td_array = array();
3 c7 p7 L; [5 w' ~2 c5 ^( ~
! f( H( W8 o9 z        foreach ($table as $key=>$tr) {
3 p5 {4 ]* _1 h4 g$ ~; b9 b' c                $td = explode('{td}', $tr);# e' h% v/ l& V( k
                array_pop($td);5 d* W9 N/ x: _; {4 x& D
                $td_array[] = $td;. p0 M. q( t6 l3 z1 B( y* V
        }# k3 p% B& N' @- O4 h
        return $td_array;
  E! j$ E+ q3 c+ w}4 X  v3 }- W1 K- P/ A& o* l6 ?3 d& E
//下載HTML網頁
  Y: @9 Y3 t3 p4 H2 Q3 `9 ]4 K$s=file_get_contents('http://chengji.lideping.com:7000/exam/query/query.jsp?logname=20133011235');
, _$ T- }8 Z# O. a+ Q% L$s=mb_convert_encoding("$s", "UTF-8", "GBK");  //編碼轉換
0 k4 d; b# {! C8 t% S% q$ l//獲取最後一個table內容
$ U% H0 B/ f% K7 G1 I- O/ N, E$s=substr($s,strrpos($s,'<table'));
2 w3 Z1 L: z2 u. [5 I# J$ Y$s=substr($s,0,strpos($s,'</table>')+8);
5 S  k* ]/ e* P//刪除超鏈接
: q, n( e  u# H$s=preg_replace('|<a href.*?</a>|','',$s);# Y5 q- b# l# o
$s=preg_replace('|<td align=\'center\'></td>|','',$s);//刪除多餘空格* t. c7 r. l; c8 o
- k/ V4 m( k/ r5 k
echo $s;
" R' U& h! X' ^" m9 c% opreg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出
* Z1 W; C# }5 j  P- ~: F8 k4 z& s0 h# V! @% _) m
$arr = get_td_array($table[0][0]);//執行函數
1 H, h- h& U- S* c0 O0 pprint_r($arr);) T7 L' d5 k5 P  [7 d0 U; M# o
?>
' Q# E! s1 P  `$ T0 z7 S</body>
9 s- ^% i2 c$ q8 ^. {. k</html>4 Q# U2 ~- z5 V4 A2 a. X
[/mw_shl_code]4 P# R& w/ m  q- I, C

6 X0 B& L2 E5 R" M( w' t: N7 G; @6 A5 o已調整完全正確輸出,給樓主你註釋了哪句代碼的問題
6 a- n$ F1 M8 S3 g: d4 p" w

評分

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

查看全部評分

回復

使用道具 舉報

發表於 2014-10-22 13:53:15 | 顯示全部樓層
記得給懸賞!  20分啊,口水流出來了' ^! o) u1 i  ^0 O% g

點評

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

使用道具 舉報

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

本版積分規則

QQ|小黑屋|點基

GMT+8, 2026-11-4 13:45

DZ支援

數位遊牧BBS

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