outputting websites using curl_multi with preg_match_all, site loads slow
I have a script using curl-multi开发者_C百科 and it scraps through multiple sites for contents. I was wondering what would be a faster way to output certain contents for each site.
<?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;
}
?>
Need help making it so the page won't load long. this is only a few which I will need to add more to.
Can you try this code ?
<?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;
?>
精彩评论