2012-01-03 127 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