2011-03-14 118 views
0

我有一個使用curl-multi的腳本,它通過多個站點爲內容剪貼。我想知道爲每個網站輸出特定內容的更快方式。使用curl_multi與preg_match_all輸出網站,網站加載速度慢

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

需要幫助,使頁面不會加載太久。這只是我需要添加更多內容的一小部分。

+0

**不要使用正則表達式來解析HTML **,使用[HTML DOM解析器來代替(http://simplehtmldom.sourceforge.net/) – 2011-08-18 00:22:32

回答

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; 
?> 
 
+0

優秀!謝謝 – hellomello 2011-03-31 23:33:04