如何从网站上使用php获取数据并将其数据放入变量?

时间:2014-07-20 10:17:57

标签: php fetch explode

我将阅读网站数据并将其数据放入变量中。

$site=file_get_contents($url);

我将使用$ site变量并从$ url中读取数据:

例如,我将从此网址获取数据 http://en.wikipedia.org/wiki/List_of_states_and_territories_of_the_United_States

我该怎么做?

我使用了爆炸功能,但我想得到一个div的中心,例如当有一个div我需要得到div中的内容

<div id="div1">
...I need to get here between open and close tag of div
</div>

1 个答案:

答案 0 :(得分:0)

使用正则表达式:

<?php
$url = "http://somesite.com/";

$begining_match = '<body>';
$end_match = '</body>';

$file_contents = file_get_contents($url);

$extracted_contens = preg_replace("/\$begining_match([a-z0-9\.])\$end_match/i", "\\1", $file_contents);

echo $extracted_contents;
?> 

取自:http://www.webdeveloper.com/forum/showthread.php?37464-possible-to-have-file_get_contents-only-grab-between-two-things