2012-01-03 155 views
-3

可能重複:
How to parse and process HTML with PHP?提取網頁內容

<body> 
    <table align="center"> 

<? 
$ip=$_SERVER['REMOTE_ADDR']; 
$url=file_get_contents("http://whatismyipaddress.com/ip/$ip"); 
preg_match_all('/<th>(.*?)<\/th><td>(.*?)<\/td>/s',$url,$output,PREG_SET_ORDER); 
for ($q=0; $q < 25; $q++) { 
    if ($output[$q][1]) { 
     if (!stripos($output[$q][2],"Blacklist")) { 
      echo "<tr><td>".$output[$q][1]."</td><td>".$output[$q][2]."</td></tr>"; 

     } 
    } 
} 
?> 
    </table> 
</body> 

現在,有了這個代碼,我得到了很多像ISP,國家信息等

如何提取內容以便將它們加載到我的數據庫中,如$ country,$ isp等?我認爲它可以做到,如果它被轉換爲XML,但我不知道。

回答

2

您正在尋找PHP簡單的HTML DOM解析器

使用,你可以:

$html = file_get_html('http://www.google.com/'); 
從解析器定義如下發現並得到別人的個人標籤使用HTML功能

後來..

Read this

+0

你會給我一個小例子...示例代碼...我不明白 – user889030 2012-01-03 18:33:28

+1

哥們閱讀我給出的鏈接有很多例子...閱讀文檔更多.. – 2012-01-03 18:35:51

+0

http://simplehtmldom.sourceforge.net/manual.htm – 2012-01-03 18:36:52