Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php从url以更快的方式获取内容_Php_Fopen_File Get Contents_Readfile - Fatal编程技术网

使用php从url以更快的方式获取内容

使用php从url以更快的方式获取内容,php,fopen,file-get-contents,readfile,Php,Fopen,File Get Contents,Readfile,我正在使用php,我想以更快的方式从url获取内容。 这是我使用的一个代码。 代码:1 如果我像上面那样做,那么我就走错了方向,因为我已经在变量$content中获得了完整内容,然后对其进行了修改。 这里可以是任何函数方法或任何其他直接从url获取纯html文本的方法 下面的代码只是为了理解而写的,这不是php的原始代码。 理想编码:3 如果我能得到这个函数,它将比其他函数快得多。可以吗?这是可能的。 谢谢。您可以使用正则表达式删除css脚本的标记和图像的标记,只需将这些代码替换为空白即可 pr

我正在使用php,我想以更快的方式从url获取内容。 这是我使用的一个代码。 代码:1

如果我像上面那样做,那么我就走错了方向,因为我已经在变量$content中获得了完整内容,然后对其进行了修改。 这里可以是任何函数方法或任何其他直接从url获取纯html文本的方法

下面的代码只是为了理解而写的,这不是php的原始代码。 理想编码:3

如果我能得到这个函数,它将比其他函数快得多。可以吗?这是可能的。
谢谢。

您可以使用正则表达式删除css脚本的标记和图像的标记,只需将这些代码替换为空白即可

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

有关函数的更多详细信息,请转到此处:

您可以使用正则表达式删除css脚本的标记和图像的标记,只需将这些代码替换为空白即可

preg_replace($pattern, $replacement, $string);
有关函数的更多详细信息,请点击此处:

试试这个

$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);
}
更多信息-

试试这个

$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);
}

更多信息-

jaD您像代码2一样问我,请看我的问题。以下是这不好的原因。谢谢。@user2280065,从中您无法选择要获取什么或不获取什么。每当您发送获取页面的请求时,它每次都会发送整个页面。您可以做的是缓存之类的事情。保存最常使用的页面。jaD您像代码2一样问我。请查看我的问题。以下是这不好的原因。谢谢。@user2280065,从中您无法选择要获取什么或不获取什么。每当您发送获取页面的请求时,它每次都会发送整个页面。您可以做的是缓存之类的事情。保存最常用的页面。该页面http://www.filehippo.com 已经在其中嵌入了脚本和样式。您不能选择不下载,但可以对其进行筛选。页面http://www.filehippo.com 已经在其中嵌入了脚本和样式。您不能选择不下载它,但可以对它进行筛选。不需要使用$this,当它是简单的代码片段时,可以在类外使用。或者至少将其转换为示例类,以便未经经验的复制粘贴不会出错。不需要使用$this,当它是简单的代码段时,可以在类外使用。或者至少将其转换为示例类,以便未经经验的复制粘贴不会出错。
$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);
}