过期域名预定抢注

 找回密碼
 免费注册

求大神。。。

[複製鏈接]
發表於 2014-10-21 21:56:41 | 顯示全部樓層 |閱讀模式
懸賞20點點已解決
本帖最後由 xx19941215 於 2014-10-21 22:02 編輯 ( a) W, l' i$ ?. I8 l$ {; g

. b5 y  w& z( u1 s# {1 `4 w6 T代碼[mw_shl_code=php,true]<?php1 S9 L( K% i$ ~6 ?. _
function get_td_array($table) {
+ }6 E6 N3 G2 S6 ]        $table = preg_replace("'<table[^>]*?>'si","",$table);
& }/ \/ u# R. K        $table = preg_replace("'<tr[^>]*?>'si","",$table);
) U8 Z# O, I" B; q+ _  e5 b3 |7 ?        $table = preg_replace("'<td[^>]*?>'si","",$table);3 b+ ^1 u) v# _. [3 G, m# i* x
        $table = str_replace("</tr>","{tr}",$table);+ J1 d( T% g! D
        $table = str_replace("</td>","{td}",$table);) \' y4 h& n. \1 S: }0 Y7 v
        //去掉 HTML 標記9 _- I7 [% \6 f  _; |3 [! n& X1 G
        $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
# Y9 q" t5 W1 z        //去掉空白字符: _8 E3 i( p" a2 l+ E/ r
        $table = preg_replace("'([rn])+'","",$table);
: j( y; u# [0 [' f5 S        $table = preg_replace('/ /',"",$table);
; s7 \6 \! f& s/ n9 Q2 }        $table = str_replace(" ","",$table);
3 T: C6 j& F) @/ `7 E  h& I        $table = str_replace(" ","",$table);
  Z+ R/ E0 K% ]% m/ F        $table = explode('{tr}', $table);
8 `- e3 G6 i) w        array_pop($table);; M5 t* t+ X: v3 p* R
        foreach ($table as $key=>$tr) {3 F4 ^0 K- P% F: k0 c! h
                $td = explode('{td}', $tr);
5 X. i8 G; s) p( L$ \, a                array_pop($td);
) H1 Z/ I& m- ]4 o                $td_array[] = $td;
. K+ `# X" O) f( x4 V. }        }
8 \# \8 h. t$ f0 B/ {; p# c        return $td_array;  X) r6 ?7 `: \: }9 |4 X: c, B
}
) D0 d* P& x6 V' |* t  O3 a  //下載HTML網頁
/ r, K5 U$ H! _2 \9 N( {  $s=file_get_contents('http://chengji.lideping.com:7000/exam/query/query.jsp?logname=20133011235');
* ~' T" ?* L9 y7 Q  $s=mb_convert_encoding("$s", "UTF-8", "GBK");  //編碼轉換
- @) \+ f' s/ m: w  //獲取最後一個table內容9 {% @: G2 s" `. z+ x
  $s=substr($s,strrpos($s,'<table'));; o* a8 f1 @0 w( [8 j* R
  $s=substr($s,0,strpos($s,'</table>')+8);9 T- J4 {$ I/ b6 e" B) n- l. {
  //刪除超鏈接3 p, @7 n2 K& M$ U( U7 d) R4 ]
  $s=preg_replace('|<a href.*?</a>|','',$s);  C9 G) N) [$ p' N
$s=preg_replace('|<td align=\'center\'></td>|','',$s);//刪除多餘空格2 O: u) q, N( }

% `- o# {2 j' y' U! u+ r7 Z; X  echo $s;
) M5 j; L: m& o/ _2 H) \  preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出3 N! ?3 ?4 o$ a
  $arr = get_td_array($table[0][0]);//執行函數/ K- O- x5 E8 \7 R# D
  print_r($arr);
& T$ r: W- ]# e6 N. g9 ]+ }/ ?; c?>2 n$ @, f. O$ K- z$ p
[/mw_shl_code] 函數是將表格轉換為數組的函數,網上找的。。問題是: 2014-10-21_220010.jpg
8 f9 U7 P5 K. _" T, i: j, u7 _8 H5 J: G
: ?. R' x9 L( j' [, p

最佳答案

查看完整內容

樓主結賬! 人呢

點評

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

使用道具 舉報

發表於 2014-10-21 21:56:42 | 顯示全部樓層
樓主結賬!                      人呢7 t* J- D1 [; B5 J7 ?& p
回復

使用道具 舉報

 樓主| 發表於 2014-10-21 22:14:25 | 顯示全部樓層
[mw_shl_code=php,true]<?php0 F9 Q8 [# Q' D# Q# e, B# g
header('Content-Type:text/html;charset:Utf-8');
5 u1 }' K0 `( X1 [: E; u& S5 _function get_td_array($table) {
, ?1 @' o- T) j" N$ \7 {& A        $table = preg_replace("'<table[^>]*?>'si","",$table);4 P3 O. l: D/ W# g9 p# O9 e) w
        $table = preg_replace("'<tr[^>]*?>'si","",$table);
2 Z7 E3 v0 y8 |# U5 n9 D        $table = preg_replace("'<td[^>]*?>'si","",$table);
1 J1 {8 I  O+ ~        $table = str_replace("</tr>","{tr}",$table);  o: O& w; W1 s. @3 ]0 M
        $table = str_replace("</td>","{td}",$table);
" i' B7 P" c0 \, @1 A6 e& E; G        //去掉 HTML 標記
7 `, T2 m4 }6 i! t5 i$ |        $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
6 m4 t( N. M  F9 E4 f, f7 n% t; v        //去掉空白字符" `5 j' V+ T0 u
        $table = preg_replace("'([rn])+'","",$table);
4 X' ]7 M- [1 e/ z& |        $table = preg_replace('/ /',"",$table);
+ t5 S7 z" h2 ]" ]5 b7 T: k. I        $table = str_replace(" ","",$table);3 r$ x9 q) k+ L
        $table = str_replace(" ","",$table);  Y- A8 V9 g1 x  u( p" }) a4 z& l  q
        $table = explode('{tr}', $table);+ Z' I1 @+ C$ r; ~2 Q" Z  o
        array_pop($table);
; L! Y- I# j( L1 ]  z. C  F# z        foreach ($table as $key=>$tr) {4 X3 c' r; |; O: `! ?  H: |9 m
                $td = explode('{td}', $tr);4 @$ g  B7 L5 B) e
                array_pop($td);
3 p3 c$ B+ R7 ]                $td_array[] = $td;
6 [! p  {4 E" x0 @4 i8 f) }0 l7 t        }7 c6 O  U: a5 A: S7 a) {0 N( v
        return $td_array;5 S" |: l8 B5 a2 @
} 3 y9 A3 M( E# ~: m
  //下載HTML網頁
# P  a# F8 w( H- ?  $s=file_get_contents('http://222.88.107.92/exam/query/query_detail.jsp?examid=2014102014&paperid=100094');' d" U0 X- b. ]& |
  $s=mb_convert_encoding("$s", "UTF-8", "GBK");  //編碼轉換
6 M6 z  v1 i; |1 Y  //獲取最後一個table內容
% o: t- s: ]4 m, \! U8 V5 k: y/ F  $s=substr($s,strrpos($s,'<table'));
6 @. d0 |' R" h- Y* U  $s=substr($s,0,strpos($s,'</table>')+8);1 i  o. _: h3 R) K5 n$ V6 z
  //刪除超鏈接
' P, s: F% ]* i; E' t  $s=preg_replace('|<a href.*?</a>|','',$s);
  E6 S3 ?& N! L  $s=preg_replace('|<td align="center" width="120">圖片</td>|','',$s);8 f4 |# F" e5 f
  $s=preg_replace('|<td align=\'center\'></td>|','',$s);
8 z0 h2 u& ?& O0 j8 a8 X  $s=preg_replace('|<td align="center" width="120">評閱老師</td>|','',$s);% J2 }7 _/ \: A5 f/ O. O# b
  $s=preg_replace('|<td align=\'center\'>110(.*?)<br></td>|','',$s);
) _6 W9 K: c4 s" f$ ^3 J6 Z  echo $s;
1 h8 |7 S! W+ H( {  preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出
  L  t( f; q+ G  $arr = get_td_array($table[0][0]);//執行函數
8 d' v* f. x) K: M; }# x9 M9 K  print_r($arr);, Z: W+ z. h0 {! i
?>2 b& C( \2 L0 h" `- Y7 S
[/mw_shl_code] 2014-10-21_221318.jpg
' q3 k) R( M9 \& i1 x
回復

使用道具 舉報

 樓主| 發表於 2014-10-21 22:15:00 | 顯示全部樓層
一樣的代碼,上面的這個就輸出了完全的數組
- B, L! g$ k( Q4 Y5 _* q4 c
回復

使用道具 舉報

發表於 2014-10-22 13:52:01 | 顯示全部樓層
[mw_shl_code=php,true]<!DOCTYPE html>) b- h8 a, p2 G8 v" X
<html lang="zh-cn">
) }' R7 H( a. O" t9 {; u; U<head>; C' \1 Z, I0 |; a' h, j
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
' f( Y1 h, t: |! Z) a8 F& o+ m<title>測試</title>  B+ Z( t3 Z) h6 L* ]9 F, i8 b
</head>1 r2 ?: w/ p$ G4 v1 }9 G3 a
<body>
# a/ \( K6 j3 ?3 L  O/ b. y3 G<?php" l- a& g. v  Y4 ~8 J% @
function get_td_array($table) {0 L& u3 O* k# k% H  j
        //去掉table標籤上的屬性( |+ n3 e. I. O  ^) e: U
        $table = preg_replace("'<table[^>]*?>'si","",$table);6 a8 c' t4 Q- o
        //去掉TR上的屬性2 G! T' R% F) S$ U- ?# m, G
        $table = preg_replace("'<tr[^>]*?>'si","",$table);: L; a% f$ u( g$ L0 a3 A
        //去掉td上的屬性
8 w3 S& V  {) _        $table = preg_replace("'<td[^>]*?>'si","",$table);
* o* U; i" D: ?9 s        //標籤替換" X' A6 ]5 G6 Z/ L; z8 k/ I( }; f% n
        $table = str_replace("</tr>","{tr}",$table);
( J/ C; \+ N  n        $table = str_replace("</td>","{td}",$table);: R. D% E0 @# {  _

! A' X: K$ Y9 n! W! S3 n. B5 h* v$ w1 @& m        //去掉 HTML 標記! x# f% q% G/ Y% j. o1 ?- P  s
        $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
) ]7 {+ l0 `* J# G$ D        //去掉空白字符
& |& A0 y( U, a  ^6 y" f: d5 E$ S( q        $table = preg_replace("'([rn])+'","",$table);
' L- b! l3 H' F# E: e        $table = preg_replace('/ /',"",$table);
7 m/ s2 Z; B! _* ]5 ?0 s; Z5 q. K- ?        $table = str_replace(" ","",$table);
- h% L, ^' z* K4 a3 @( D$ `6 l# U  q        $table = str_replace(" ","",$table);6 g0 q- c0 t. Y
        $table = explode('{tr}', $table);
; U- B3 ^' C8 i3 s/ s) X0 O/ ?/ Z/ ?, r
        //這裡打印出來是長度為2的數組。
1 @" q( z. I  R        print_r($table);
, ?' u' ^! S# {8 z1 _        //這句代碼有問題,這個會把數組的第二個元素刪掉,所以得不到想要的結果4 {$ y+ v( R7 R7 O0 I( ^- [
        //array_pop($table);' n6 D5 i, u& L( P$ `
        ( ]1 _/ ^4 L4 i5 K& C- F$ x. m# e
        $td_array = array();" k$ T, F) f0 c2 r7 }
$ M( R2 X  w  K+ ^2 S, Y
        foreach ($table as $key=>$tr) {- o4 z, E! W7 G3 l) G
                $td = explode('{td}', $tr);* x3 c. D) X- f* k( f6 X
                array_pop($td);# v+ j# R4 ~5 O; n
                $td_array[] = $td;
9 L- r5 q0 ^& h$ f2 }        }; s& K8 J( b) w9 g: T1 z8 P. _
        return $td_array;+ `) L8 s/ w& e8 Y" |* N  S, Q
}& S; e% e/ h# W5 ]; f
//下載HTML網頁
$ n, y) x% N# I1 z) A& X, P( Y$s=file_get_contents('http://chengji.lideping.com:7000/exam/query/query.jsp?logname=20133011235');: t7 @- y3 a  C, ~
$s=mb_convert_encoding("$s", "UTF-8", "GBK");  //編碼轉換
+ k3 F. b. s3 b3 j0 S, f: Y3 Y5 b//獲取最後一個table內容
  l  t; t. k" L+ W" D1 B2 R$s=substr($s,strrpos($s,'<table'));& r+ a1 s  b3 W. T
$s=substr($s,0,strpos($s,'</table>')+8);( ^/ k8 r( y* {( Y, u3 T7 u
//刪除超鏈接
  t8 t! e" c' u$s=preg_replace('|<a href.*?</a>|','',$s);8 P: G9 V' \1 E; Q
$s=preg_replace('|<td align=\'center\'></td>|','',$s);//刪除多餘空格0 j! \" X" a) M! y# y  ]: ^+ Z8 d

3 A4 I8 f1 ?& y7 Xecho $s;/ S1 e, ^; M) O6 M. j2 N' C
preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出
& h1 i# [6 D$ T
/ h# a$ H1 [6 n2 K) @$arr = get_td_array($table[0][0]);//執行函數
& p6 t2 K- e1 m: V' v+ kprint_r($arr);( n/ Q' Z4 o9 J: K
?>2 ?  ?) t3 w7 V6 @! O
</body>4 m; U9 |- n4 \4 T7 H
</html>: F6 u3 @7 Q1 p9 }9 o+ P6 u
[/mw_shl_code], r$ ~$ M+ B8 {7 M8 @
1 T- _( V: g" P2 r( E( z
已調整完全正確輸出,給樓主你註釋了哪句代碼的問題! t) i8 {: B9 A* c, ]3 W0 }

評分

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

查看全部評分

回復

使用道具 舉報

發表於 2014-10-22 13:53:15 | 顯示全部樓層
記得給懸賞!  20分啊,口水流出來了( @4 ^2 O0 _: J& F: ]) _

點評

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

使用道具 舉報

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

本版積分規則

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

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

GMT+8, 2025-7-15 10:01

By DZ X3.5

小黑屋

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