过期域名预定抢注

 找回密碼
 免费注册

求大神。。。

[複製鏈接]
發表於 2014-10-21 21:56:41 | 顯示全部樓層 |閱讀模式
懸賞20點點已解決
本帖最後由 xx19941215 於 2014-10-21 22:02 編輯
6 }$ I5 g- G+ Z+ ^. z) ^2 q1 H9 P+ i; H- u
代碼[mw_shl_code=php,true]<?php
# u0 f) Q/ u/ @5 tfunction get_td_array($table) {7 o8 G& q3 p* a
        $table = preg_replace("'<table[^>]*?>'si","",$table);
$ v4 u" O& t! W# C/ |  \. g+ J        $table = preg_replace("'<tr[^>]*?>'si","",$table);
( S. m7 Y% H3 Z        $table = preg_replace("'<td[^>]*?>'si","",$table);
! G; H& }# E. v2 _0 D        $table = str_replace("</tr>","{tr}",$table);$ C# y' p& L( k& E$ s- |
        $table = str_replace("</td>","{td}",$table);& F1 x! T5 g2 H- ]* q, i& X
        //去掉 HTML 標記& O- ^; J  M9 o/ p, \/ m
        $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);4 O; W3 C/ {  A  u) H
        //去掉空白字符
+ u1 j: S) B# a6 f. Q        $table = preg_replace("'([rn])+'","",$table);
# e. w2 z8 a9 ?( I# h8 q# o% K: i        $table = preg_replace('/ /',"",$table);
( z% h; Z5 P. l        $table = str_replace(" ","",$table);( H2 p4 A# p. Q5 j1 A+ q5 g' q# s' s( V
        $table = str_replace(" ","",$table);! T! o7 F) N5 U* e( Z
        $table = explode('{tr}', $table);" {& a" d% j7 A* m: |; A6 V) B, }
        array_pop($table);
: L: W9 b$ r2 M4 D4 o: x/ G        foreach ($table as $key=>$tr) {- H/ A( w; c! w( X5 y, i( i
                $td = explode('{td}', $tr);6 N6 l6 n& e- L2 G: o  d
                array_pop($td);
' G2 n8 g& z0 Y# I# H# u                $td_array[] = $td;
& R9 h% |( W/ p' z! K6 f$ y% _% q        }
% H: L; _" k: h- I0 ]' j        return $td_array;
% A- L* d1 F# \/ q}
9 h$ R# _8 [5 i( r( P# v  //下載HTML網頁" J8 `! {- B4 a% j% \, o6 O
  $s=file_get_contents('http://chengji.lideping.com:7000/exam/query/query.jsp?logname=20133011235');3 v* t" }$ f* M' w! z  I0 L5 a/ b
  $s=mb_convert_encoding("$s", "UTF-8", "GBK");  //編碼轉換: M, q$ K: c1 U
  //獲取最後一個table內容, f5 H" T; G$ k1 ^) A! B- e. l5 c
  $s=substr($s,strrpos($s,'<table'));
8 R7 j2 W+ H" R0 c0 A  $s=substr($s,0,strpos($s,'</table>')+8);
2 A% M. |( s6 ^" U. i  //刪除超鏈接
) `' S0 V  q3 i& g2 \* v  $s=preg_replace('|<a href.*?</a>|','',$s);
; N( N9 W( z+ e6 y5 [4 S! B  r$s=preg_replace('|<td align=\'center\'></td>|','',$s);//刪除多餘空格
! @' f: c- n5 K8 x$ b7 Z$ ^6 q6 @+ Q
  echo $s;
$ H: G) E% a1 @1 P9 M" t; B7 V1 i# V  preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出# o( {" d: c" D% D) P  e
  $arr = get_td_array($table[0][0]);//執行函數4 `4 s* O" ]0 p2 D
  print_r($arr);. r& W& x% T+ e: d* f# b5 e+ D9 s8 v
?>
' k) \0 H" J- K* Q[/mw_shl_code] 函數是將表格轉換為數組的函數,網上找的。。問題是: 2014-10-21_220010.jpg

7 X; w$ Z. n) A+ \
; T* r% n* \2 f" `, i) H* j6 a  J

最佳答案

查看完整內容

樓主結賬! 人呢

點評

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

使用道具 舉報

發表於 2014-10-21 21:56:42 | 顯示全部樓層
樓主結賬!                      人呢
# u5 B& D! c/ P7 T2 N
回復

使用道具 舉報

 樓主| 發表於 2014-10-21 22:14:25 | 顯示全部樓層
[mw_shl_code=php,true]<?php
- i4 `+ Z. y; `. N5 Rheader('Content-Type:text/html;charset:Utf-8');
$ D% d( n) ]5 [- U4 {function get_td_array($table) {
: e. E5 }9 i8 H8 h% j        $table = preg_replace("'<table[^>]*?>'si","",$table);5 g9 f7 |; v0 X8 g$ u
        $table = preg_replace("'<tr[^>]*?>'si","",$table);2 p+ l3 \" {( a9 `; f( F  v
        $table = preg_replace("'<td[^>]*?>'si","",$table);' \" Q- v- \3 E& X. q3 k7 p
        $table = str_replace("</tr>","{tr}",$table);$ Y4 V, e3 O) s' P+ H3 b$ K2 C, G
        $table = str_replace("</td>","{td}",$table);
3 q; O: E7 ?* d0 c. b. N        //去掉 HTML 標記; \0 D: d. ^+ D' G
        $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
2 @8 r" a9 G% V# o! P        //去掉空白字符, e) T  `/ K9 U6 r6 h" l
        $table = preg_replace("'([rn])+'","",$table);" V8 T5 I. j* x9 p! T7 F6 ~6 R5 f
        $table = preg_replace('/ /',"",$table);& a4 \8 X0 }# {$ s7 }
        $table = str_replace(" ","",$table);
7 c6 ?6 r/ f7 w5 R; d9 l        $table = str_replace(" ","",$table);
" ?  L  |4 ?& K" B        $table = explode('{tr}', $table);3 T3 R) i- T& u" j
        array_pop($table);( `5 Q4 b, `2 j, Z2 o; J$ y
        foreach ($table as $key=>$tr) {
" p: T' L3 p  ]7 k; d% _3 |                $td = explode('{td}', $tr);7 r3 U, D1 c2 g" ]* c+ k
                array_pop($td);
% h9 ^. f% ]2 K4 E9 w                $td_array[] = $td;
' F9 A' p1 I" S+ ^        }
+ s2 @0 w+ a# e; _        return $td_array;8 w( x) X" S" v0 n$ I
}
: |" \' H3 c& X, [" H' n( z  //下載HTML網頁
& [7 _6 N( u' D& i' @- n$ H, O: b6 Z  $s=file_get_contents('http://222.88.107.92/exam/query/query_detail.jsp?examid=2014102014&paperid=100094');; e# `) C9 i& ?  R
  $s=mb_convert_encoding("$s", "UTF-8", "GBK");  //編碼轉換# x& F5 o, t. ]& v9 K
  //獲取最後一個table內容
) j6 Y, L- e3 l1 E3 U! T  $s=substr($s,strrpos($s,'<table'));- s( V$ d9 q, N( U1 V
  $s=substr($s,0,strpos($s,'</table>')+8);: y% f6 _  a9 ^4 ]; e& O% Y0 f
  //刪除超鏈接
: D3 y" ~1 r1 W4 b6 ?. @  $s=preg_replace('|<a href.*?</a>|','',$s);
& n7 k/ v3 ]. I% s7 W  $s=preg_replace('|<td align="center" width="120">圖片</td>|','',$s);
8 N+ X  a6 x( h4 ?/ g4 ~7 `  $s=preg_replace('|<td align=\'center\'></td>|','',$s);7 x* F3 `0 g/ N) r6 y, b1 N
  $s=preg_replace('|<td align="center" width="120">評閱老師</td>|','',$s);
9 _( f6 ~6 z2 h0 h9 N  $s=preg_replace('|<td align=\'center\'>110(.*?)<br></td>|','',$s);
1 l% d, n. [' a: E, l" x5 _2 S  echo $s;6 E/ s: F; d8 E; d
  preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出
' K, ^% \( k. l- p7 m, `9 O# k/ f  $arr = get_td_array($table[0][0]);//執行函數
) t! w4 j: }* |4 H& }  print_r($arr);
1 O: E* O2 e$ h4 e* b# Y?>
* b8 B' o+ U+ E$ h! M* D( }/ k+ n' v[/mw_shl_code] 2014-10-21_221318.jpg % O) J; ?4 [+ K
回復

使用道具 舉報

 樓主| 發表於 2014-10-21 22:15:00 | 顯示全部樓層
一樣的代碼,上面的這個就輸出了完全的數組
) |2 r& M3 x$ N! T
回復

使用道具 舉報

發表於 2014-10-22 13:52:01 | 顯示全部樓層
[mw_shl_code=php,true]<!DOCTYPE html>
  W( R( F+ Q* T6 g2 R) S9 W* y! }6 h<html lang="zh-cn">
  F0 s& o% J' W9 y# E1 t<head>8 k) c/ ^7 _. L3 h* Z& G  @$ e# {, S% K
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />* P: P9 ^# g3 C" Z
<title>測試</title>$ Z8 {0 l1 m$ Y
</head>; P2 A5 r- P; s5 Y) q
<body>
7 L: S; P- c2 L* Q! G$ o<?php$ N& E6 G! o' r- U0 O
function get_td_array($table) {
- S0 R' {" p( }+ u1 T6 J        //去掉table標籤上的屬性
8 S! r( j) U& P' {        $table = preg_replace("'<table[^>]*?>'si","",$table);+ A- B4 {5 d. d3 y
        //去掉TR上的屬性+ v; |) ~1 X$ a' r) I
        $table = preg_replace("'<tr[^>]*?>'si","",$table);- n' I5 H, V* I" ?' n
        //去掉td上的屬性
3 S, g! k! V$ i$ M- K4 O" V        $table = preg_replace("'<td[^>]*?>'si","",$table);
; `: `$ f2 s& U6 }/ N9 a5 ^        //標籤替換4 U- K! I' Z; b. x3 t
        $table = str_replace("</tr>","{tr}",$table);
. s! w' c' S7 H) ^) l% m+ M  H: r" b        $table = str_replace("</td>","{td}",$table);6 y2 ~" X. ?. y

! `& W/ z) O9 f6 t* w% D        //去掉 HTML 標記5 n6 l1 _+ j  H* a! y3 q5 y
        $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);4 ~- w( _, |  ?/ ~6 ^1 Y, U
        //去掉空白字符
/ o  z) g9 f1 U/ }/ Y9 J& ?7 U4 {        $table = preg_replace("'([rn])+'","",$table);* V3 u2 N/ ?5 {+ U0 q/ z5 f
        $table = preg_replace('/ /',"",$table);0 P% r, F$ Q- Q4 v% Y3 P" [
        $table = str_replace(" ","",$table);! v' r2 H$ p( S4 C* B6 g
        $table = str_replace(" ","",$table);
& H; C9 N0 \" `4 ?" f        $table = explode('{tr}', $table);
! B/ W" k/ z0 U7 D7 p
4 z8 }) T$ `& J" m+ ?        //這裡打印出來是長度為2的數組。- `, o6 Z/ h* Q. [5 e* [
        print_r($table);+ [) Z2 U- P2 a) W; t8 `8 p! N
        //這句代碼有問題,這個會把數組的第二個元素刪掉,所以得不到想要的結果" A4 Q; ], Z9 \" F
        //array_pop($table);; J7 P! [3 x3 @# c7 w" d8 N" _
       
( w* Z% Z3 s; v5 d: \. f- j        $td_array = array();& T3 a4 ^# ?3 i. F
' ?: |, u% d$ [. U7 J# Q5 O, I
        foreach ($table as $key=>$tr) {
2 y9 ]9 T; S) v7 }% f                $td = explode('{td}', $tr);
- y7 H9 A9 ]' L3 S. p                array_pop($td);
& r5 N: {+ c4 P/ X4 Y                $td_array[] = $td;
: U9 Y1 p1 ?: ~8 \, m        }# y+ o( M. x- R. \# R% S' o
        return $td_array;, }1 x4 b, l$ Z! E9 @3 B
}
& M) N; b3 M' u& C/ v4 O//下載HTML網頁* N6 e8 l' v, V( k8 I! U
$s=file_get_contents('http://chengji.lideping.com:7000/exam/query/query.jsp?logname=20133011235');
/ _! s4 ?4 ?% @1 E6 m$s=mb_convert_encoding("$s", "UTF-8", "GBK");  //編碼轉換2 E; T% {3 S5 r) [
//獲取最後一個table內容! b. G5 l4 i* U% S7 ]2 W) V+ \
$s=substr($s,strrpos($s,'<table'));
4 E0 K* H2 z: q7 b$s=substr($s,0,strpos($s,'</table>')+8);
6 v& Y$ ^! B2 y) ?, R% Q//刪除超鏈接* y2 ^9 O5 D1 H  p4 \  O( L+ p
$s=preg_replace('|<a href.*?</a>|','',$s);& l. a% @& h) J) J2 ~! G* @. v
$s=preg_replace('|<td align=\'center\'></td>|','',$s);//刪除多餘空格
' U! M# R, h0 l' S, ^% W2 A
5 z# m9 Y: A6 @3 g: Techo $s;( b0 S0 s. l0 T- ]/ L, G' U
preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正則表達式將課表的表格取出9 n0 w" R) N# s5 i, U5 G* P7 D
; d0 [1 Y- q8 i) G8 t5 a8 _) P
$arr = get_td_array($table[0][0]);//執行函數
: {' Q, K- T+ Q; n) ]( B% [1 p. _print_r($arr);2 S& h6 a1 ?+ W8 D; ?0 _# R9 E
?>1 l+ p, N5 m, T& F" P
</body>  u6 Z+ z  |( _7 n. r
</html>
3 L9 K5 R6 r9 B0 p' T9 Z[/mw_shl_code]) v7 P: ?& x% @. F

* w# H- F  [. R0 b0 F已調整完全正確輸出,給樓主你註釋了哪句代碼的問題
" q( v/ V  U7 M2 ]

評分

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

查看全部評分

回復

使用道具 舉報

發表於 2014-10-22 13:53:15 | 顯示全部樓層
記得給懸賞!  20分啊,口水流出來了! p9 t7 L$ y. l8 V. Y9 Y; v5 o

點評

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

使用道具 舉報

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

本版積分規則

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

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

GMT+8, 2025-5-11 21:45

By DZ X3.5

小黑屋

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