01.<?php
02.//利用別人的網站自己寫的一個查詢火車時刻表的小程式
03.if(isset($_POST['cx'])){
04.//拼字網址,測試能否順利開啟
05.$url = "http://wap.huoche.com.cn/chaxun/result.php?txtchufa=".$_POST['txtchufa']."&txtdaoda=".$_POST['txtdaoda']; //目標站
06.$fp = @fopen($url, "r") or die("逾時");
07.//抓取內容
08.$content=file_get_contents($url);
09.//處理內容
10.$str = $content;
11.$start = "<ul>";
12.$end = "</ul>";
13.function get_sub_content($str, $start, $end){
14. if ( $start == '' || $end == '' ){
15. return;
16. }
17. $str = explode($start, $str);
18. $str = explode($end, $str[1]);
19. return $str[0];
20.}
21.$row = get_sub_content($str, $start, $end);
22.//去掉a標籤
23.$out_a = str_ireplace("</a>","",preg_replace("/<a[^>]+>/", "", $row));
24.//去掉li標籤
25.$out_td = str_ireplace("</li>","",preg_replace("/<li[^>]+>/", "", $out_a));
26.//存放到數組
27.$over = explode('<li>', $out_td);
28.
29.}
30.?>
31.<form method="post" >
32.出發站:(漢字或拼音首字母)<br />
33.<input type="text" name="txtchufa" value="" /><br />
34.到達站:(例如:北京 或 BJ)<br />
35.<input type="text" id="to" name="txtdaoda" value="" /><br />
36.<input type="submit" name="cx" value="查詢" />
37.</form>
38.<?if($over){?>
39.<table>
40.<?foreach($over as $key=>$value){?>
41.<tr>
42.<td><?=$value?></td>
43.</tr>
44.<?}?>
45.</table>
46.<?}?>