I am scrapping some results, when I scrape, I am getting 50 results. I want to show only first 20 results, below is the code, how to limit results?
require 'simple_html_dom.php';
$url = "http://espncricinfo.com/scores/match-100007";
$html = file_get_html( $url );
$posts = $html->find('div[type=tuple]');
foreach ( $posts as $post ) {
$matchtitle = $post->find('span[class=desigd]',0);
$matchdate = $post->find('span[class=date]',0);
echo $matchtitle;
echo $matchdate;
}
Use a variable to keep counting the results, and break from the for loop as soon as the count is 20
.
<?php
require 'simple_html_dom.php';
$url = "http://espncricinfo.com/scores/match-100007";
$html = file_get_html( $url );
$posts = $html->find('div[type=tuple]');
$count = 0;
foreach ( $posts as $post ) {
$count++
$matchtitle = $post->find('span[class=desigd]',0);
$matchdate = $post->find('span[class=date]',0);
echo $matchtitle;
echo $matchdate;
if($count == 20)
{
exit();
}
}
?>