<?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