2013-05-27 50 views
0

我正在使用php,我想以更快的方式從url獲取內容。
這是我使用的代碼。
代碼:(1)使用php以更快的方式獲取內容使用php

<?php 
    $content = file_get_contents('http://www.filehippo.com'); 
    echo $content; 
?> 

這是很多其他方法來讀取文件,如fopen()readfile()等,但我認爲file_get_contents()比這些方法快。

在我上面的代碼中,當你執行它時,你會發現它從本網站的所有東西甚至圖像和廣告。我只想得到計劃HTML文本沒有CSS樣式,圖像和廣告。我怎樣才能得到這個。
看到這個瞭解。
CODE:(2)

<?php 
    $content = file_get_contents('http://www.filehippo.com'); 
    // do something to remove css-style, images and ads. 
    // return the plain html text in $mod_content. 
    echo $mod_content; 
?> 

如果我是這樣做上述然後我會在錯誤的方式,因爲我已經得到變量$content的全部內容,然後修改它。
這裏可以是任何函數方法或其他任何從url直接獲取純文本html文本的方法。

下面的代碼只是爲了理解而寫的,這不是原來的php代碼。
IDEAL CODE:(3);

<?php 
    $plain_content = get_plain_html('http://www.filehippo.com'); 
    echo $plain_content; // no css-style, images and ads. 
?> 

如果我能得到這個功能,它會比別人快得多。這可能嗎?
謝謝。

+0

頁面'HTTP:// www.filehippo.com'嵌入了已經腳本和樣式。你不能選擇不下載它,但你可以過濾它。 –

回答

3

試試這個。

$content = file_get_contents('http://www.filehippo.com'); 
$this->html = $content; 
$this->process(); 
function process(){ 

    // header 
    $this->_replace('/.*<head>/ism', "<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE html PUBLIC '-//WAPFORUM//DTD XHTML Mobile 1.0//EN' 'http://www.wapforum.org/DTD/xhtml-mobile10.dtd'><html xmlns='http://www.w3.org/1999/xhtml'><head>"); 

    // title 
    $this->_replace('/<head>.*?(<title>.*<\/title>).*?<\/head>/ism', '<head>$1</head>'); 

    // strip out divs with little content 
    $this->_stripContentlessDivs(); 

    // divs/p 
    $this->_replace('/<div[^>]*>/ism', '') ; 
    $this->_replace('/<\/div>/ism','<br/><br/>'); 
    $this->_replace('/<p[^>]*>/ism',''); 
    $this->_replace('/<\/p>/ism', '<br/>') ; 

    // h tags 
    $this->_replace('/<h[1-5][^>]*>(.*?)<\/h[1-5]>/ism', '<br/><b>$1</b><br/><br/>') ; 


    // remove align/height/width/style/rel/id/class tags 
    $this->_replace('/\salign=(\'?\"?).*?\\1/ism',''); 
    $this->_replace('/\sheight=(\'?\"?).*?\\1/ism',''); 
    $this->_replace('/\swidth=(\'?\"?).*?\\1/ism',''); 
    $this->_replace('/\sstyle=(\'?\"?).*?\\1/ism',''); 
    $this->_replace('/\srel=(\'?\"?).*?\\1/ism',''); 
    $this->_replace('/\sid=(\'?\"?).*?\\1/ism',''); 
    $this->_replace('/\sclass=(\'?\"?).*?\\1/ism',''); 

    // remove coments 
    $this->_replace('/<\!--.*?-->/ism',''); 

    // remove script/style 
    $this->_replace('/<script[^>]*>.*?\/script>/ism',''); 
    $this->_replace('/<style[^>]*>.*?\/style>/ism',''); 

    // multiple \n 
    $this->_replace('/\n{2,}/ism',''); 

    // remove multiple <br/> 
    $this->_replace('/(<br\s?\/?>){2}/ism','<br/>'); 
    $this->_replace('/(<br\s?\/?>\s*){3,}/ism','<br/><br/>'); 

    //tables 
    $this->_replace('/<table[^>]*>/ism', ''); 
    $this->_replace('/<\/table>/ism', '<br/>'); 
    $this->_replace('/<(tr|td|th)[^>]*>/ism', ''); 
    $this->_replace('/<\/(tr|td|th)[^>]*>/ism', '<br/>'); 

    // wrap and close 

} 
private function _replace($pattern, $replacement, $limit=-1){ 
    $this->html = preg_replace($pattern, $replacement, $this->html, $limit); 
} 

更多 - https://code.google.com/p/phpmobilizer/

+0

無需使用$ this,當它是簡單的代碼片段就可以在課堂外使用。或至少將其轉換爲示例類,以便無經驗的複製粘貼不會出錯。 –

+0

這就是爲什麼我只在代碼下面添加詳細信息鏈接。 –

0

,您可以使用正則表達式來刪除CSS腳本的標籤和圖像的標籤,只需用空格

preg_replace($pattern, $replacement, $string); 

替代那些代碼更詳細的功能去這裏:http://php.net/manual/en/function.preg-replace.php

+0

http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 –

+0

** jaD **你問我喜歡** code(2 )**請看我的問題。這是爲什麼這不好。謝謝。 – Axeem

+0

@ user2280065,從http://www.filehippo.com你不能選擇得到什麼或不能。每當您發送請求獲取http://www.filehippo.com頁面時,它都會每次發送整個頁面。 你可以做的就像緩存。保存最常用的頁面。 –