รับเนื้อหาในวิธีที่เร็วกว่าจาก url โดยใช้ php

ฉันใช้ php ฉันต้องการรับเนื้อหาจาก url อย่างรวดเร็ว
นี่คือโค้ดที่ฉันใช้
รหัส:(1)

<?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 แล้วจึงแก้ไขมัน
นี่คือวิธีฟังก์ชันใดๆ หรืออย่างอื่นก็ได้ที่ได้รับข้อความ html ธรรมดาโดยตรงจาก url .

โค้ดด้านล่างเขียนขึ้นเพื่อให้เข้าใจเท่านั้น นี่ไม่ใช่โค้ด php ดั้งเดิม
รหัส IDEAL:(3);

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

ถ้าผมได้ฟังก์ชั่นนี้มา มันจะเร็วกว่าฟังก์ชั่นอื่นๆ มาก เป็นไปได้ไหม
ขอบคุณ


person Axeem    schedule 27.05.2013    source แหล่งที่มา
comment
หน้า http://www.filehippo.com มีสคริปต์และสไตล์ฝังอยู่ภายในแล้ว คุณไม่สามารถเลือกที่จะไม่ดาวน์โหลดได้ แต่คุณสามารถกรองได้   -  person Dave Chen    schedule 27.05.2013


คำตอบ (2)


ลองสิ่งนี้

$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/

person Jerin K Alexander    schedule 27.05.2013
comment
ไม่จำเป็นต้องใช้ $this เมื่อเป็นโค้ดง่ายๆ สามารถใช้นอกคลาสได้ หรืออย่างน้อยก็แปลงเป็นคลาสตัวอย่าง ดังนั้นการคัดลอกและวางที่ไม่มีประสบการณ์จะไม่ทำให้เกิดข้อผิดพลาด - person kuldeep.kamboj; 27.05.2013

คุณสามารถใช้นิพจน์ทั่วไปเพื่อลบแท็กของ css-script และแท็กของรูปภาพได้ เพียงแทนที่โค้ดเหล่านั้นด้วยช่องว่าง

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

ดูรายละเอียดฟังก์ชั่นเพิ่มเติมได้ที่นี่: http://php.net/manual/en/function.preg-replace.php

person jad-panda    schedule 27.05.2013
comment
stackoverflow.com/questions/1732348/ - person Dave Chen; 27.05.2013
comment
jaD คุณกำลังถามฉันเช่น code(2) โปรดดูคำถามของฉัน นี่คือเหตุผลว่าทำไมสิ่งนี้ถึงไม่ดี ขอบคุณ - person Axeem; 27.05.2013
comment
@ user2280065 จาก filehippo.com คุณไม่สามารถเลือกได้ว่าจะรับหรือไม่รับ เมื่อใดก็ตามที่คุณส่งคำขอรับหน้า filehippo.com ก็จะส่งทั้งหน้าทุกครั้ง สิ่งที่คุณสามารถทำได้คือบางอย่างเช่นการแคช บันทึกหน้าที่ใช้บ่อยที่สุด - person jad-panda; 27.05.2013