如何限制phpdom解析结果
how to limit php dom parsing results
我正在抓取一些结果,当我抓取时,我得到了 50 个结果。我想
只显示前 20 个结果,下面是代码,如何限制结果?
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;
}
使用一个变量来持续计算结果,并在计数到20
时立即退出for循环。
<?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();
}
}
?>
我正在抓取一些结果,当我抓取时,我得到了 50 个结果。我想 只显示前 20 个结果,下面是代码,如何限制结果?
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;
}
使用一个变量来持续计算结果,并在计数到20
时立即退出for循环。
<?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();
}
}
?>