过期域名预定抢注

 找回密碼
 免费注册

求大神。。。

[複製鏈接]
發表於 2014-10-21 21:56:41 | 顯示全部樓層 |閱讀模式
懸賞20點點已解決
本帖最後由 xx19941215 於 2014-10-21 22:02 編輯
2 G7 l$ I' K/ ?  k: Z: `3 O5 _4 F; N9 S9 m) b% K
代碼[mw_shl_code=php,true]<?php
2 ?7 T/ H  {8 gfunction get_td_array($table) {
. p. f& |, I1 @4 E' b, o        $table = preg_replace("'<table[^>]*?>'si","",$table);7 n" ]( |; w5 B) K$ M5 v. t  j
        $table = preg_replace("'<tr[^>]*?>'si","",$table);' a2 J" z! U: p( R
        $table = preg_replace("'<td[^>]*?>'si","",$table);
2 c+ A1 C6 E- ?! l# z, m        $table = str_replace("</tr>","{tr}",$table);8 B3 m+ G- l  J& A
        $table = str_replace("</td>","{td}",$table);
1 Z# U1 L1 F" @9 q. j* P        //去掉 HTML 標記
) ]0 h% ?, y3 |1 t        $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);0 W) ~. T. V3 l# l0 t2 d
        //去掉空白字符
" `7 M* L( D& [5 p" F  [8 ?* E+ Q        $table = preg_replace("'([rn])+'","",$table);  i3 K) U1 A( J% U% ]
        $table = preg_replace('/ /',"",$table);( z1 j6 N% G4 x6 N8 m
        $table = str_replace(" ","",$table);8 N" U* U8 h8 P$ J& e% _& s
        $table = str_replace(" ","",$table);
- z9 ]8 [( t/ C% Q' L# o        $table = explode('{tr}', $table);7 _' S  M5 c+ H# c) P, l4 v
        array_pop($table);  T1 {# X5 c( ]
        foreach ($table as $key=>$tr) {
# x! B1 `( y4 _9 L+ j6 I                $td = explode('{td}', $tr);
4 L; _9 X- j' `5 |! z                array_pop($td);, |5 s/ p  e% t/ B. K3 b7 l% Z2 _
                $td_array[] = $td;2 v; a# R7 |/ l
        }
2 x) \: @. E2 o2 H* F        return $td_array;
$ ^# I" T, R& t  x- ^3 v) f} 6 M+ D6 I2 i" \" ?
  //下載HTML網頁
; X. K1 H# B' T4 A# [% U4 l  $s=file_get_contents('http://chengji.lideping.com:7000/exam/query/query.jsp?logname=20133011235');
8 |* t7 r4 Y4 _9 H' T1 ~" ^  $s=mb_convert_encoding("$s", "UTF-8", "GBK");  //編碼轉換
$ R. @# y" j2 Z  //獲取最後一個table內容
; k0 t3 h1 m% P; t. M3 \  $s=substr($s,strrpos($s,'<table'));3 k+ C0 U( L3 f. `9 {* K
  $s=substr($s,0,strpos($s,'</table>')+8);. _" j6 ~  J* R9 m, ~
  //刪除超鏈接
( k' Z- O- ^) {- g1 E  $s=preg_replace('|<a href.*?</a>|','',$s);
' ?0 y0 |" k  s$ G$s=preg_replace('|<td align=\'center\'></td>|','',$s);//刪除多餘空格% G, [1 y; u) }) f( y

9 ~% R! u! b# @  i2 P! i  echo $s;
, M! k. K' M+ d; X2 q: `; |1 D9 w! u  preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出+ u1 \6 j1 l' \  J6 s+ G; H; z( f
  $arr = get_td_array($table[0][0]);//執行函數
. B7 ]/ A; A( I. b" t; T7 N" S  print_r($arr);
; Q$ d2 _% [3 l1 J8 ]?>$ [( L, K9 o# b# B* _9 d
[/mw_shl_code] 函數是將表格轉換為數組的函數,網上找的。。問題是: 2014-10-21_220010.jpg

/ `, P% ?( X3 E8 }- a# F: j4 n7 [* Q& Y+ o+ `( p1 Z

最佳答案

查看完整內容

樓主結賬! 人呢

點評

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

使用道具 舉報

發表於 2014-10-21 21:56:42 | 顯示全部樓層
樓主結賬!                      人呢
$ W+ R7 U4 Q4 d* ]+ R
回復

使用道具 舉報

 樓主| 發表於 2014-10-21 22:14:25 | 顯示全部樓層
[mw_shl_code=php,true]<?php
6 F7 E1 Y- o! j4 B# w" J, C1 Iheader('Content-Type:text/html;charset:Utf-8');8 q8 E) D  t: ]% H! w
function get_td_array($table) {1 w6 X% a7 _' k1 P7 H1 ^
        $table = preg_replace("'<table[^>]*?>'si","",$table);# M  S8 B8 a. Z% N* u8 I
        $table = preg_replace("'<tr[^>]*?>'si","",$table);; P- ?9 Y2 K3 D8 Y' S
        $table = preg_replace("'<td[^>]*?>'si","",$table);
/ S7 B* w. o5 v        $table = str_replace("</tr>","{tr}",$table);
) J  [- h3 o0 l        $table = str_replace("</td>","{td}",$table);* [- Q5 \9 v+ ^1 `5 S& T
        //去掉 HTML 標記
2 P7 u1 Y. Z- h; p0 [        $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
0 Z. l- x! ~# r8 W0 b! `        //去掉空白字符4 T5 I6 u) N+ s* X4 X. O6 s: p! l
        $table = preg_replace("'([rn])+'","",$table);$ E/ e$ o6 o; x/ J3 P! j# Y
        $table = preg_replace('/ /',"",$table);6 H# D) D! n( Q2 d9 b7 D5 P
        $table = str_replace(" ","",$table);" V0 }4 x1 Z# V; B: G( P+ m2 `
        $table = str_replace(" ","",$table);" g9 v/ P0 A  X( w) e& ~/ H1 j
        $table = explode('{tr}', $table);
% }* j$ u( r- L+ H% R" Y        array_pop($table);' p; F) g/ K9 N: T. ~9 y, G; Y
        foreach ($table as $key=>$tr) {
/ o; \( t; O/ N  l; x4 y. p2 L                $td = explode('{td}', $tr);( N5 ~& t# q! S) K4 S, f- y5 T$ C! ]
                array_pop($td);3 V2 `  q! J% ]2 r! \/ [0 z$ I
                $td_array[] = $td;4 ~; r- X4 _! @2 b! x4 a
        }1 }+ Z: p: s) T' v# P" n/ P
        return $td_array;) R6 R8 ^! p8 \& x- P3 {0 }* }) C
}
; S! s/ ^3 d$ e; s4 J5 e7 ]  //下載HTML網頁
* E5 L$ ~, Q. d2 T  $s=file_get_contents('http://222.88.107.92/exam/query/query_detail.jsp?examid=2014102014&paperid=100094');! Q8 a" h6 U8 ~' l/ W3 H4 M- S6 }
  $s=mb_convert_encoding("$s", "UTF-8", "GBK");  //編碼轉換
. y1 |6 _: P* d: z6 B! O7 W* P  //獲取最後一個table內容! @7 j; H" u' w5 q/ E. l  ^
  $s=substr($s,strrpos($s,'<table'));
: \4 r' Q7 U, p6 Z  $s=substr($s,0,strpos($s,'</table>')+8);
# Y) ?5 R: m7 T! [, X0 ?* E  //刪除超鏈接
  }+ e- U7 }/ g% R% ~  $s=preg_replace('|<a href.*?</a>|','',$s);% o- }' }" l" Y) B- S; [% x
  $s=preg_replace('|<td align="center" width="120">圖片</td>|','',$s);' ]& A7 b, p* p8 J! [- [9 {" ]
  $s=preg_replace('|<td align=\'center\'></td>|','',$s);
2 h5 S1 w3 y* f$ j! s! Q, ~) c$ `  $s=preg_replace('|<td align="center" width="120">評閱老師</td>|','',$s);
5 }: B  i# ?9 c' ?  $s=preg_replace('|<td align=\'center\'>110(.*?)<br></td>|','',$s);
* D; p9 p. M- b3 M$ d  echo $s;1 U1 M0 K/ z" ?! b- F
  preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出& I6 i7 D2 [- R* i
  $arr = get_td_array($table[0][0]);//執行函數
( N" y0 t% C/ j' x6 o3 D9 K  print_r($arr);
% q  W( ~: f$ s, D9 j?>% \  c) T7 g9 s" h- A( s
[/mw_shl_code] 2014-10-21_221318.jpg , o& W! P& q* X0 Z* r
回復

使用道具 舉報

 樓主| 發表於 2014-10-21 22:15:00 | 顯示全部樓層
一樣的代碼,上面的這個就輸出了完全的數組- |, l3 ~/ a8 V4 R4 O7 y% E
回復

使用道具 舉報

發表於 2014-10-22 13:52:01 | 顯示全部樓層
[mw_shl_code=php,true]<!DOCTYPE html>  C# @8 d6 w1 Z+ W# W
<html lang="zh-cn">* O% D1 V$ Q  \/ U. _
<head>0 p& M* V, _) k5 _1 N
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
; c3 h+ J! N7 ~/ x5 l<title>測試</title>
1 R  p! b/ v  ~' s5 m</head>0 ~+ E# X9 p6 z! B, {3 P6 `% [
<body>7 q7 n7 c4 E% v' m* \: }* ]3 m3 W
<?php
  ?* W) r* v2 _3 Y5 n. u' ~7 ufunction get_td_array($table) {1 h  l! X( D+ N$ U
        //去掉table標籤上的屬性' x: t3 R* J7 [
        $table = preg_replace("'<table[^>]*?>'si","",$table);" _* B! c# Z& ~4 G
        //去掉TR上的屬性; f6 ~+ }9 J6 k9 G: K. Z5 |( |
        $table = preg_replace("'<tr[^>]*?>'si","",$table);& Y% {  C: Z: j% {( T5 F& |
        //去掉td上的屬性! i: A6 d9 R+ Z' [- F3 X3 e' N- T" m
        $table = preg_replace("'<td[^>]*?>'si","",$table);
6 B! l; ~! |7 r        //標籤替換
# t2 V  m" @% v  `' x* ]        $table = str_replace("</tr>","{tr}",$table);
: Q( i5 p  j% l6 O$ E& W, I        $table = str_replace("</td>","{td}",$table);
! X+ H0 E, j9 L- h8 _+ f! W+ f, r/ o2 F, Y: T4 a0 i
        //去掉 HTML 標記
/ ]( s' _; j6 ~- ^! o* @2 q        $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
& ?# L3 g0 Z, ^4 |9 n        //去掉空白字符  J7 s; V: e9 o* k- Y+ ^
        $table = preg_replace("'([rn])+'","",$table);
, n# R7 g  R) r5 v: O1 X        $table = preg_replace('/ /',"",$table);9 \% ?' c" q# F! C8 x1 {" o
        $table = str_replace(" ","",$table);
, Z9 A4 L4 ]( R* v+ S/ y        $table = str_replace(" ","",$table);
2 h; m  w$ f% m4 s        $table = explode('{tr}', $table);
; Z0 n, B2 h/ ?
; ?( Y8 K# i' ]5 l, K" J/ f        //這裡打印出來是長度為2的數組。3 O* c- ], F, R$ n0 [
        print_r($table);
3 D5 q3 B* }1 z( d) g, x        //這句代碼有問題,這個會把數組的第二個元素刪掉,所以得不到想要的結果
5 ?+ `7 l' T0 w3 l$ x. M        //array_pop($table);# i9 }) Z# ]. g* \6 R3 ]: G
       
0 G6 e$ C4 ?5 ]6 g        $td_array = array();
6 @; l- z& n9 ?  C
# {: ^% S, T7 Z; M6 N        foreach ($table as $key=>$tr) {
" v. r& h  \" V- k6 @( e0 N, p                $td = explode('{td}', $tr);
6 _3 B! W6 R' Y                array_pop($td);1 V' h4 Y" o9 C& J2 c6 G
                $td_array[] = $td;) Z/ j9 [: |+ H  J, {
        }
1 f3 G: {  Z* t1 S        return $td_array;
6 ^1 p" V& r5 g9 Z" U! f}7 y0 X: S1 ^8 E  J; u
//下載HTML網頁. M3 T. n4 S: n2 m. i  d" w
$s=file_get_contents('http://chengji.lideping.com:7000/exam/query/query.jsp?logname=20133011235');2 x1 {' o6 N3 W' o  \4 _% [* m
$s=mb_convert_encoding("$s", "UTF-8", "GBK");  //編碼轉換4 q1 P1 _# p, ^( {7 Q
//獲取最後一個table內容
5 _" u' k7 X; r' ~3 [$s=substr($s,strrpos($s,'<table'));
. ]2 [% T5 q* z6 ]. o0 ~6 _; o$s=substr($s,0,strpos($s,'</table>')+8);
& K1 c8 Y2 P  i; s//刪除超鏈接
" q* D( P# z4 V, m! O' q+ a) Y8 `; z$s=preg_replace('|<a href.*?</a>|','',$s);
/ U- S, l' [" s/ H2 _0 j* T/ X$s=preg_replace('|<td align=\'center\'></td>|','',$s);//刪除多餘空格
+ C: l4 \# b5 H/ B: d8 V$ S) c& @: R" w- Q$ ]! ]
echo $s;
' n& }5 N9 b* n# Q- r7 Ypreg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出1 A$ Q1 \0 n& e+ O$ h' }
! ^/ Y+ n: w- Z  t5 D, \( V0 T
$arr = get_td_array($table[0][0]);//執行函數' |6 W- Q* e: Y- P
print_r($arr);
; Q- @* z1 N' ~' Q- o- g" l& c8 q?>) N$ e- J$ Z4 H8 n
</body>( `. y; r& [$ {5 r/ v% v
</html>* G% |$ U' Q1 ^  W# o, y# B% B
[/mw_shl_code]
) m3 x' C0 n" W& J! K; N1 M
$ ]" z. v0 U% }1 M3 }5 R0 n& I已調整完全正確輸出,給樓主你註釋了哪句代碼的問題
+ E; ?; W& T) D

評分

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

查看全部評分

回復

使用道具 舉報

發表於 2014-10-22 13:53:15 | 顯示全部樓層
記得給懸賞!  20分啊,口水流出來了' z* C: S8 V7 o" h  s6 P7 R

點評

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

使用道具 舉報

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

本版積分規則

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

QQ|小黑屋|點基

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

DZ支援

數位遊牧BBS

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