正在加载
请稍等

菜单

红楼飞雪 梦

15526773247

文章

Home web服务器端开发 php天气预报小模块,内含table解析类
Home web服务器端开发 php天气预报小模块,内含table解析类

php天气预报小模块,内含table解析类

web服务器端开发 by

php天气预报小模块,内含table解析类[@more@]

 

 

  1. <?PHP
  2. /** ensure this file is being included by a parent file */
  3. //defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
  4. //$count = intval( $params->get( 'url', 57029 ) );
  5. $today = DATE("Y_m_d");
  6. $weatherfile = $today.".html";
  7. IF (IS_FILE($weatherfile) && 1==0) {
  8. $file = FILE_GET_CONTENTS($weatherfile);
  9. ECHO $file;
  10. }
  11. ELSE{
  12. $city = 58357;//58357苏州
  13. $imgroot = 'http://weather.china.com.cn/';
  14. $url ='http://weather.china.com.cn/city/'.$city.'_full.html';
  15. //$url="w.html";
  16. $file=FILE_GET_CONTENTS($url);
  17.  
  18. $from='<table width="90%" border="0" cellspacing="0" cellpadding="0">'; //开始截取的HTML代码
  19. $start = '<table id="china_weather">';
  20. $to='</table>'; //结束截取的HTML代码
  21. $file = STRSTR($file,$from);
  22. $topos = STRPOS($file,$to);
  23. $file = SUBSTR($file,0,$topos+STRLEN($to));
  24. $table = STR_REPLACE($from,$start,$file);
  25. $ystable = $table;
  26.  
  27. //echo $table;
  28. $mtable = NEW managetable;
  29. $tr = $mtable->get_htmltag_innerText($table,'tr');
  30. $cells = $mtable->get_td_innerText($tr);
  31. //print_R($cells);
  32.  
  33. $exceptTag = 'img'; //不允许清理的tag
  34. $cells = $mtable->clear_td_tags($cells,$exceptTag);
  35. //print_R($cells);
  36.  
  37. $city = $cells[0][0];
  38. $img = $cells[0][1];
  39. $src = $mtable->cut($img,""",""",'in');
  40. $img = '<img src="'.$imgroot.$src.'"/>';
  41. $degree = $cells[1][0];
  42. $descrip = $cells[1][1];
  43. $weather = '<marquee class="weather" width="200">'.$city.$img.$degree.$descrip.'</marquee>';
  44. ECHO $weather;
  45.  
  46. $fp = FOPEN($weatherfile,"wb");
  47. FWRITE($fp,$weather);
  48.  
  49. }//end else
  50.  
  51. /**
  52. * class managetable Short description. this class is used to manage htmltable string.
  53. */
  54. CLASS managetable
  55. {
  56. FUNCTION clear_td_tags(&$cells,$extag){
  57. $exre = "/<".$extag.".[^>]+>/i";
  58. //print_R($cells);
  59. FOR ($i=0,$n=COUNT($cells);$i<$n;$i++) {
  60. FOR ($j=0,$nj=COUNT($cells[$i]);$j<$nj;$j++) {
  61. $cell = $cells[$i][$j];
  62. IF (PREG_MATCH($exre,$cell,$matches)) {
  63. //echo "$extag:this tag find $matches[0] n ";
  64. }
  65. ELSE {
  66. $cells[$i][$j] = $this->clearTag($cell);
  67. //echo "not find:".$cells[$i][$j][$k] ."n";
  68. }
  69. }//for 2
  70. }//for 1
  71. RETURN $cells;
  72. }//end func
  73.  
  74. FUNCTION clearTag($html){
  75. $re = "/<.[^>]+>/i";
  76. $htmlclean = PREG_REPLACE($re,"",$html);
  77. RETURN $htmlclean;
  78. }//end func
  79.  
  80. FUNCTION get_td_innerText(&$tr){
  81. $cells = ARRAY();
  82.  
  83. IF (IS_ARRAY($tr)) {
  84. FOR ($i=0,$n=COUNT($tr);$i<$n;$i++) {
  85. $td = $this->get_htmltag_innerText($tr[$i],'td');
  86. FOR ($j=0,$m=COUNT($td);$j<$m ;$j++ ) {
  87. $cells[$i][$j]= $td[$j];
  88. }
  89.  
  90. }//end for
  91. RETURN $cells;
  92. }//end if
  93. ELSE {
  94. RETURN FALSE;
  95. }
  96. }//end funct gettd
  97.  
  98. FUNCTION get_htmltag_innerText(&$html,$htmltag){
  99. $from = '<'.$htmltag;
  100. $fromend = '>';
  101. $to ='</'.$htmltag.'>';
  102. //print_R($html);
  103. //echo "转换标记:".$htmltag."n<br>";
  104. $selfend = ARRAY('img','br'); //自闭合标记,self end htmltag
  105. IF (IN_ARRAY($htmltag,$selfend)) {
  106. ECHO 'htmltag is self ended tag,no innerText';
  107. RETURN ;
  108. }
  109. ELSE {
  110. $strleft = STRSTR($html,$from);
  111. $i=0;
  112.  
  113. WHILE ($strleft<>'') {
  114. $topos = STRPOS($strleft,$to);
  115. $fromendpos = STRPOS($strleft,$fromend);
  116. $taginnerText[] = SUBSTR($strleft,$fromendpos+STRLEN($fromend),$topos-$fromendpos-STRLEN($fromend));
  117. $html = SUBSTR($strleft,$topos+STRLEN($to));
  118. //echo $taginnerText[0];echo $html;die;
  119. $strleft = STRSTR($html,$from);
  120. }//end while
  121. }//else 非自闭合标记
  122. RETURN $taginnerText;
  123. }//end func
  124.  
  125. FUNCTION get_htmltag_outerText(&$html,$htmltag){
  126. $from = '<'.$htmltag;
  127. $fromend = '>';
  128. $to ='</'.$htmltag.'>';
  129. $selfend = ARRAY('img','br'); //自闭合标记,self end htmltag
  130. IF (IN_ARRAY($htmltag,$selfend)) {
  131. $to = '/>';
  132. $strleft = STRSTR($html,$from);
  133. $j=0;
  134. WHILE ($strleft<>'') {
  135. $topos = STRPOS($strleft,$to);
  136. $tagouterText[] = SUBSTR($strleft,0,$topos+STRLEN($to));
  137. $html = SUBSTR($strleft,$topos+STRLEN($to));
  138. //echo $taginnerText[0];echo $html;die;
  139. $strleft = STRSTR($html,$from);
  140. }//end while
  141. }//处理自闭合标记结束,operat self end htmltag finished.
  142. //处理非自闭合标记
  143. ELSE{
  144. //echo "转换标记:".$htmltag."n<br>";//$i=0;
  145. $strleft = STRSTR($html,$from);
  146. WHILE ($strleft<>'') {
  147. $topos = STRPOS($strleft,$to);
  148. $fromendpos = STRPOS($strleft,$fromend);
  149. $tagouterText[] = SUBSTR($strleft,0,$topos+STRLEN($to));
  150. $html = SUBSTR($strleft,$topos+STRLEN($to));
  151. //echo $taginnerText[0];echo $html;die;
  152. $strleft = STRSTR($html,$from);
  153.  
  154. }//end while
  155. }//end else not self end htmltag,非自闭合标记处理结束
  156.  
  157. RETURN $tagouterText;
  158.  
  159. }//end func
  160.  
  161.  
  162. /**
  163. * Short description.
  164. */
  165. FUNCTION cut($str,$from,$to,$direct='out')
  166. {
  167. //echo "$str n $from n $to n";
  168. //$from = """; $to = """;
  169. $frompos = STRPOS($str,$from);
  170. $topos = STRPOS($str,$to,$frompos+STRLEN($from));
  171. IF($direct == 'in'){
  172. $start = $frompos+STRLEN($from);
  173. $end = $topos-$start;
  174. $txt = SUBSTR($str,$start,$end);
  175. }
  176. ELSE {
  177. $start = $frompos;
  178. $end = $topos+STRLEN($to)-$frompos;
  179. $txt = SUBSTR($str,$start,$end);
  180. }
  181.  
  182. RETURN $txt;
  183. } // end func cut
  184.  
  185. } // end class
  186.  
  187. ?>

 

07 2015-05

 

我要 分享

 

 

本文 作者

 

相关 文章