<?php echo file_get_contents ("http://www.google.com/"); ?>
但我只想获取标签在url中的内容......怎么做......? 我需要回显标签之间的内容....不是整个页面<?php echo file_get_contents如何获取某个标签中的内容
<?php echo file_get_contents ("http://www.google.com/"); ?>
但我只想获取标签在url中的内容......怎么做......? 我需要回显标签之间的内容....不是整个页面<?php echo file_get_contents如何获取某个标签中的内容
请参阅此PHP manual和cURL这也可以帮助你。
您也可以使用用户定义函数,而不是file_get_contents()函数的:
function get_content($URL){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $URL);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo get_content('http://example.com');
希望,这将解决您的问题。
我想你想从文件中的特定html标记中提取内容。为此,您可以使用正则表达式。但是查看以下链接来解析HTML文档文件:
libxml_use_internal_errors(true);
$url = "http://stackoverflow.com/questions/15947331/php-echo-file-get-contents-how-to-get-content-in-a-certain-tag";
$dom = new DomDocument();
$dom->loadHTML(file_get_contents($url));
foreach($dom->getElementsByTagName('a') as $element) {
echo $element->nodeValue.'<br/>';
}
exit;
更多信息:http://www.php.net/manual/en/class.domdocument.php
那里你可以看到如何通过id
或class
,如何让元素的选择元素属性值等
注:这是更好通过cURL
获得内容,而不是的get_file_contents
。例如:
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
还要注意,在一些网站上,你必须指定一个像CURLOPT_USERAGENT
等选项,否则内容可能不会返回。
下面是其他选项:http://www.php.net/manual/en/function.curl-setopt.php
你是什么意思“在URL标记的内容”? – Barmar 2013-04-11 11:16:09
使用curl读取数据,有时file_get_contents不起作用。 – Neo 2013-04-11 11:16:45
您需要解析代码并获取标签中的内容 – alwaysLearn 2013-04-11 11:16:53