找優仕美清潔公司
大掃除免煩惱!清潔打蠟、消毒殺菌
油漆粉刷加入客源網
每天多接2個客戶,做生意就是這麼簡單

首頁  •  j2h 論壇 • 程式設計討論     • 

preg_match_all() 取出網頁資料

房東:台灣人
發表時間:2007-05-11



// 下載網頁
$handle = fopen("http://www.30p.com.tw/House/House_Info.aspx?house_no=1041994", "r");
$contents = fread($handle, 10000 );
fclose($handle);

// 取出資料
$result = array();
preg_match_all("/([^<]+)<\/span>/", $contents, $result, PREG_SET_ORDER);

foreach ( $result as $key => $value ) {

$span_id = $result[ $key ][1];
$span_value = $result[ $key ][2];

switch ( $span_id ) {
case "lblContactName":
$lblContactName = $span_value;
break;
case "lblContactMobile":
$lblContactMobile = $span_value;
break;
case "lblContactPhone1":
$lblContactPhone1 = $span_value;
break;
case "lblContactPhone2":
$lblContactPhone2 = $span_value;
break;
case "lblContactEmail":
$lblContactEmail = $span_value;
break;
}
}

// 顯示資料
print "姓名: $lblContactName 手機: $lblContactMobile 聯絡電話(日): $lblContactPhone1 聯絡電話(夜): $lblContactPhone2 email $lblContactEmail";

?>




  • 贊助網站       

    廣利不動產-新板特區指名度最高、值得您信賴的好房仲
    您的托付,廣利用心為您服務
    廣利不動產-板橋在地生根最實在--新板特區指名度最高、值得您信賴的好房仲
    完整房訊,房屋、店面熱門精選物件,廣利不動產 優質仲介,房屋租賃、買賣資訊透明,交易真安心!

  • 1 樓住戶:仙人
    發表時間:2007-05-13
    \s\n\s+

    ob_start ();
    readfile ("http://www.168house.com/showpage.php?number=94567");
    $data = ob_get_contents ();
    ob_end_clean ();

    preg_match_all ('/\s+
    (.*)<\/font><\/td>\n\s+(.*)<\/font><\/td>\n\s+(.*)<\/font><\/td>\n\s+(.*)<\/font><\/td>\n\s+<\/tr>/i', $data, $matchse);
    print_r (trim (preg_replace ("/ /U", "", strip_tags ($matchse[0][0]))));

    2 樓住戶:阿保
    發表時間:2007-05-14

    //----- 定義要擷取的網頁地址
    $url = "http://web-address";

    //----- 讀取網頁源始碼
    $fp = file_get_contents($url);

    //----- 擷取 title 資訊
    preg_match("/(.*)<\/title>/s", $fp, $match);<br /> $title = $match[1];<br /> <br /> //----- 擷取 Description 及 Keywords<br /> $metatag = get_meta_tags($url);<br /> $description = $metatag["description"];<br /> $keywords = $metatag["keywords"];<br /> <br /> //----- 印出結果<br /> echo "URL: $url\n";<br /> echo "Title: $title\n";<br /> echo "Description: $description\n";<br /> echo "Keywords: $keywords\n";<br /> ?> <br></div></table></center><br> <center><table width="80% nowrap> <tr><td><table width="100%" CELLPADDING="0" CELLSPACING="2" bgcolor="#CEE6FD" ></td> <tr> <td align="left"><b>3 樓住戶:</b>達人<br> <b>發表時間:</b>2007-05-16 </td></TR></TABLE><table width="80%" border="0" cellspacing="0" cellpadding="3"> <div align="left"> <tr><td ><br> <?php<br /> ob_start ();<br /> readfile ("http://www.30p.com.tw/House/House_Info.aspx?house_no=1041994");<br /> $data = ob_get_contents ();<br /> ob_end_clean ();<br /> $data=strip_tags($data);<br /> $data1=explode(" ",$data);<br /> $i=1;<br /> foreach($data1 as $value)<br /> {<br /> echo "$i {$value}<br />";<br /> $i++;<br /> }<br /> ?><br></div></table></center><br> <center><table width="80% nowrap> <tr><td><table width="100%" CELLPADDING="0" CELLSPACING="2" bgcolor="#CEE6FD" ></td> <tr> <td align="left"><b>4 樓住戶:</b>老貓<br> <b>發表時間:</b>2007-08-27 </td></TR></TABLE><table width="80%" border="0" cellspacing="0" cellpadding="3"> <div align="left"> <tr><td ><br> <br /> <?php<br /> ob_start ();<br /> readfile ("http://www.591.com.tw/rent-detail-6166.html");<br /> $data = ob_get_contents ();<br /> $want = array("聯 絡 人","E- mail","行動電話","固定電話");<br /> $data = ob_get_contents ();<br /> ob_end_clean ();<br /> $body=ereg_replace( "[\"']?", "", $data);<br /> $body=str_replace("5 BR","5",$body);<br /> preg_match_all ('/<span class=style(\d+)>(.*)<\/span><\/td>/isU', $body, $matches);<br /> $total=count($matches[1]);<br /> for($i=0;$i<$total;$i++){<br /> $style = $matches[1][$i];<br /> if ($style == 10 || $style == 7 ) {<br /> $style2 = $matches[1][$i+1];<br /> if ($style2 == 5 ) {<br /> $key = strip_tags($matches[2][$i]);<br /> $value = strip_tags($matches[2][$i+1]);<br /> if (in_array($key, $want) )<br /> printf("%s:%s\n",$key,$value);<br /> <br /> $i++;<br /> }<br /> }<br /> }<br /> <br /> <br /> ?><br /> <br></div></table></center><br> <center><table width="80% nowrap> <tr><td><table width="100%" CELLPADDING="0" CELLSPACING="2" bgcolor="#CEE6FD" ></td> <tr> <td align="left"><b>5 樓住戶:</b>晴晴<br> <b>發表時間:</b>2007-08-29 </td></TR></TABLE><table width="80%" border="0" cellspacing="0" cellpadding="3"> <div align="left"> <tr><td ><br> $data = file_get_contents ("http://www.591.com.tw/rent-detail-7432.html");<br /> $pattern = '/<span class=[\'"]style(10|5)[^"\']*[\'"]>([^<]*)<\/span>/isU';<br /> $want = array("聯絡人","E-mail","行動電話","固定電話");<br /> preg_match_all($pattern,$data,$matched);<br /> <br /> foreach($matched[2] as $key=>$value){<br /> $matched[2][$key] = preg_replace('/\s/','',$matched[2][$key]);<br /> if(in_array($matched[2][$key],$want)){<br /> $result[$matched[2][$key]] = $matched[2][$key+1];<br /> }<br /> }<br /> $table = 'info';<br /> $query = sprintf("INSERT INTO %s (`聯絡人`,`E-mail`,`行動電話`,`固定電話`) VALUES ('%s','%s','%s','%s')",<br /> $table,<br /> $result['聯絡人'],<br /> $result['E-mail'],<br /> $result['行動電話'],<br /> $result['固定電話']<br /> );<br /> echo $query;<br /> <br /> if(mysql_query($query)){<br /> echo "<center><H1> 完成</h1> </center>";<br /> }else{echo "<p> 資料庫錯誤 </p>";}<br /> <br /> //有時候正規會出問題!不能用的時候請檢查正規表示式<br /> //如果無法寫入資料庫直接檢查sql語法即可<br /> <br /> <br></div></table></center><br> <center><table width="80% nowrap> <tr><td><table width="100%" CELLPADDING="0" CELLSPACING="2" bgcolor="#CEE6FD" ></td> <tr> <td align="left"><b>6 樓住戶:</b>鴨子<br> <b>發表時間:</b>2008-03-15 </td></TR></TABLE><table width="80%" border="0" cellspacing="0" cellpadding="3"> <div align="left"> <tr><td ><br> $txt = iconv("big5", "utf-8",file_get_contents("http://www.520house.com.tw/index-inquiry1-txt.asp?Key=35053&page="));<br /> $txt = str_replace("\r\n","",$txt);<br /> $txt = str_replace("<","",$txt);<br /> $txt = str_replace(">","",$txt);<br /> $txt = str_replace("\"","",$txt);<br /> $txt = str_replace(" ","",$txt);<br /> $txt = trim($txt);<br /> <br /> <br /> <br /> <br /> $re = array ("(地址:)/div/tdtdcolspan=3(.*?)/td","(建物:)/div/tdtdwidth=52%(.*?)/td","(用途:)/div/tdtdwidth=25%(.*?)/td","(所在樓層/總樓層)/div/tdtdcolspan=(.*?)/td"<br /> ,"(總價:)/div/tdtdcolspan=3(.*?)/td","(管理員:)/div/tdtd有/td","(管理費:)/div/tdtd(.*?)/td","(坪數:)/div/tdtd(.*?)/td","(電梯:)/div/tdtd(.*?)/td","(房屋格局:)/div/tdtd(.*?)/td","(屋齡:)/div/tdtd(.*?)/td","(家具:)/div/tdtdcolspan=3(.*?)/td","(詳細說明:)/div/tdtdcolspan=3(.*?)/td","(聯絡人:)/div/tdtd(.*?)/td","(身分:)/div/tdtd(.*?)/td","(市話:)/div/tdtd(.*?)/td","(手機:)/div/tdtd(.*?)/td","(電子信箱:)/div/tdtdcolspan=3(.*?)/td");<br /> <br /> <br /> <br /> $header = array();<br /> $content = array();<br /> foreach($re as $k => $v){<br /> preg_match_all("|$re[$k]|i",$txt,$tmp);<br /> array_push($header,$tmp[1][0]);<br /> array_push($content,$tmp[2][0]); <br /> }<br /> <br /> <br /> <br /> foreach($header as $k=>$v){<br /> echo $v.str_replace("br","<br>",$content[$k])."<BR>";<br /> }<br /> <br /> 幫你寫一個妳測試看看如果你的頁面本身是BIG5請將下面<br /> $txt = iconv("big5", "utf-8",file_get_contents("http://www.520house.com.tw/index-inquiry1-txt.asp?Key=35053&page="));<br /> 改成<br /> $txt = file_get_contents("http://www.520house.com.tw/index-inquiry1-txt.asp?Key=35053&page="); <br /> <br></div></table></center><br> <br><hr size=1> 共 6 人回應  選擇頁數 <a href=j2h-271-0.html>【第1 頁】 </a> <p> <table width="90%" height=65 border="1" cellpadding=1 cellspacing=0 bordercolorlight=#346783 bordercolordark=#FFFFFF bgcolor="#F0F9FF"> <tr align="center"> <td width="14%" ALIGN="left" VALIGN="TOP" NOWRAP>姓名:</td> <td align="left" colspan=3><input NAME="j2h" MAXLENGTH="16" SIZE="60" value=''> </td> </tr> <tr align="center"> <td ALIGN="left" VALIGN="TOP" NOWRAP>佈告內容:</td> <td align="left" colspan=3> <textarea name="j2h5" ROWS="15" COLS="80" ></textarea> </td> </tr> <tr> <td align=right>其他選項:</td> <td colspan=3> </td> </tr> <tr align="center"> <td ALIGN="RIGHT" VALIGN="TOP" NOWRAP colspan="4"> <p align="center"><br> <input type=submit value='確定發言' onClick="this.disable=true;"> </td></tr></table> </center></body></html>