使用curl_multi和preg_match_all输出网站,网站加载速度慢

时间:2011-03-14 23:48:11

标签: php curl foreach preg-match

我有一个使用curl-multi的脚本,它会在多个站点中搜索内容。我想知道为每个站点输出某些内容的更快方法。

<?php // Get the tables for 1 website
       preg_match_all("/\<tbody\>(.*?)\<\/tbody\>/is",
            $res[0], $matches );
       foreach($matches[0] as $value)
    {
        echo $value;
    }

    // Get the tables for site 2
       preg_match_all("/\<div class=\"dealsListS\"\>(.*?)\<\/div\>/is",
            $res[1], $matches );
       foreach($matches[0] as $value)
    {
        echo $value;
    }


    // Get the tables for site 3
       preg_match_all("/\<div class=\"city_save\"\>(.*?)\<\/div\>/is",
            $res[2], $matches );
       foreach($matches[0] as $value)
    {
        echo $value;
    }

    // Get the tables for site 4
       preg_match_all("/\<div class=\"offer-stream\"\>(.*?)\<\/div\>/is",
            $res[3], $matches );
       foreach($matches[0] as $value)
    {
        echo $value;
    }
    ?>

需要帮助才能使页面加载时间不长。这只是我需要添加的更多内容。

1 个答案:

答案 0 :(得分:1)

你能试试这段代码吗?


<?php
    function extract_data($html_code, $regex) {
       $buffer="";
       preg_match_all($regex, $html_code, $matches );
       foreach($matches[0] as $value)
       {
          $buffer .= $value;
       }
       return $buffer;
    }

    // Get the tables for 1 website
    $buffer = extract_data("/\<tbody\>(.*?)\<\/tbody\>/is", $res[0]);

    // Get the tables for site 2
    $buffer .= extract_data("/\<div class=\"dealsListS\"\>(.*?)\<\/div\>/is", $res[1]);

    // Get the tables for site 3
    $buffer .= extract_data("/\<div class=\"city_save\"\>(.*?)\<\/div\>/is", $res[2]);

    // Get the tables for site 4
    $buffer .= extract_data("/\<div class=\"offer-stream\"\>(.*?)\<\/div\>/is", $res[3]);

    echo $buffer;
?>